相关疑难解决方法(0)

F#中的重载运算符:(/)

我想重写F#中的(/)运算符以获取字符串并保留数字的含义.

/// Combines to path strings
let (/) path1 path2 = Path.Combine(path1,path2)

let x = 3 / 4 // doesn't compile
Run Code Online (Sandbox Code Playgroud)

如果我尝试以下操作,我会得到"警告29扩展成员无法提供操作员重载.请考虑将操作符定义为类型定义的一部分."

/// Combines to path strings
type System.String with
  static member (/) (path1,path2) = Path.Combine(path1,path2)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

此致,forki

f# inline operator-overloading

13
推荐指数
4
解决办法
2056
查看次数

对 F# 中任意级别嵌套的列表求和

我正在尝试创建一个 F# 函数,该函数将返回int任意嵌套的s列表的总和。IE。它适用于 a list<int>、 alist<list<int>>和 a list<list<list<list<list<list<int>>>>>>

在 Haskell 中,我会写一些类似的东西:

class HasSum a where
    getSum :: a -> Integer

instance HasSum Integer where
    getSum = id

instance HasSum a => HasSum [a] where
    getSum = sum . map getSum
Run Code Online (Sandbox Code Playgroud)

这会让我做:

list :: a -> [a]
list = replicate 6

nestedList :: [[[[[[[[[[Integer]]]]]]]]]]
nestedList =
    list $ list $ list $ list $ list $
    list $ list $ list $ list $ list …
Run Code Online (Sandbox Code Playgroud)

f# haskell

10
推荐指数
1
解决办法
272
查看次数

标签 统计

f# ×2

haskell ×1

inline ×1

operator-overloading ×1