我对列表的定义有问题.通常是列表定义为data [a] = [] | a : [a]
但如果我在我的代码上写这样的东西具体我将定义data T a = N | a -> (T a)解释器给我一个错误:
格式错误的类型或类声明的头
你知道什么是错的吗?.
我已经阅读了F#中关于价值限制的所有内容,但我仍然不理解它.我有以下代码:
type tree<'a> =
| Nil
| Node of (tree<'a> * 'a * tree<'a>)
let rec flatten = function
| Nil -> []
| Node ( Nil, b, Nil ) -> [b]
| Node ( l, h, p ) -> List.concat [(flatten l);[h];(flatten p)]
Run Code Online (Sandbox Code Playgroud)
并且编译器显示错误:
error FS0030: Value restriction. The value 'it' has been inferred to have generic type
val it : '_a list
Either define 'it' as a simple data term, make it a function with explicit arguments or, …Run Code Online (Sandbox Code Playgroud) 我试着写一个这个monad
data W x = W x [String]
instance Monad W where
return x = W x []
W a h1 >>= f = case f a of
W b h2 -> W b (h1++h2)
Run Code Online (Sandbox Code Playgroud)
但是,现在当我将使用这个monad并尝试在代码中编写return或>> =时,我会通过编译获得警告:
实例声明中没有显式方法或Prelude.return的默认方法.实例声明中没有显式方法或Prelude.>> =的默认方法.
有谁知道如何修复此警告?
非常感谢你
我有以下代码:
let rec sums1 n = function
| (a,b,x:int array,s,k) when (s<n&&b=x.Length-1) -> []//None
| (a,b,x:int array,s,k) when (a=b&&((x.Length-1)=b))->[]// None
| (a,b,x,s,k) when (s=n) -> (Array.toList(Array.sub x a k))
| (a,b,x,s,k) when (s<n) -> sums1 n (a,b+1,x,s+x.[b+1],k+1)
| (a,b,x,s,k) when (s>n) -> sums1 n (a+1,b,x,s-x.[a],k-1)
| (a,b,c,d,e) -> []//None
let neco n s =match (sums1 n (0,-1,s,0,0)) with
| [] ->None
| x ->Some x
let ssum n xs:list<int> = neco n (List.toArray xs)
Run Code Online (Sandbox Code Playgroud)
如何编译器不允许我从类型选项<list <int >>的ssum值返回.我将返回此类型,而不是其他类型.有人知道吗?十分感谢.