小编877*_*877的帖子

列表的标准定义

我对列表的定义有问题.通常是列表定义为data [a] = [] | a : [a] 但如果我在我的代码上写这样的东西具体我将定义data T a = N | a -> (T a)解释器给我一个错误:

格式错误的类型或类声明的头

你知道什么是错的吗?.

syntax haskell types list

8
推荐指数
2
解决办法
6349
查看次数

F#值限制

我已经阅读了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)

f# value-restriction

5
推荐指数
1
解决办法
1701
查看次数

Haskell - 执行错误

我试着写一个这个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.>> =的默认方法.

有谁知道如何修复此警告?

非常感谢你

monads warnings haskell

2
推荐指数
1
解决办法
267
查看次数

F#类型选项问题

我有以下代码:

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值返回.我将返回此类型,而不是其他类型.有人知道吗?十分感谢.

f# types option

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

标签 统计

f# ×2

haskell ×2

types ×2

list ×1

monads ×1

option ×1

syntax ×1

value-restriction ×1

warnings ×1