我想重写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# 函数,该函数将返回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)