我在ghci 7.6.3中尝试了以下内容
prelude> let m = map
以上工作.GHCi没有错误.
但后来我试过,
prelude> let r = read
上面的代码在GHCi中引发了一个很大的错误.这是我得到的错误,
*Main> let r = read
<interactive>:122:9:
No instance for (Read a0) arising from a use of `read'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Read IOMode -- Defined in `GHC.IO.IOMode'
instance Read BufferMode -- Defined in `GHC.IO.Handle.Types'
instance Read Newline -- Defined in `GHC.IO.Handle.Types'
...plus 30 others
In …Run Code Online (Sandbox Code Playgroud) 可以somone请告诉我为什么这段使用State monad的代码永远不会结束?
fib' :: State [Int] ()
fib' = do l <- get
let l2 = sum (last2 l)
put (l ++ [l2])
return ()
fib'
take 10 $ execState fib' [0, 1]
Run Code Online (Sandbox Code Playgroud)
当我在ghci REPL中执行它时,该函数不停止地执行.
给定一个列表lst和一个数字n,下面的代码输出一个列表,其中n包含给定列表中的不同元素lst.
rndsel :: (Eq a, RandomGen g) => [a] -> g -> Int -> [a]
rndsel lst _ 0 = []
rndsel lst g n = schar:(rndsel rem g (n-1))
where schar = lst !! index
index = head $ randomRs (0, ll-1) g
ll = length lst
rem = delete schar lst
Run Code Online (Sandbox Code Playgroud)
我想知道是否有任何方法可以改善这段代码的可读性,口才以及是否可以使它更具惯用性的haskell代码?
在GHCi提示符下,
我能做到,
Prelude Text.Parsec> :t (oneOf "aeiou")
得到 (oneOf "aeiou") :: Stream s m Char => ParsecT s u m Char
但是当我这样做的时候
let s = oneOf "aeiou"
我收到以下错误,
interactive>:36:9:
No instance for (Stream s0 m0 Char)
arising from a use of `oneOf'
Possible fix: add an instance declaration for (Stream s0 m0 Char)
In the expression: oneOf "aeiou"
In an equation for `m': m = oneOf "aeiou"
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我为什么我不能分配给s?