Haskell简单编译bug

fms*_*msf 2 haskell

我正在尝试运行此代码:

let coins = [50, 25, 10, 5, 2,1]

let candidate = 11

calculate :: [Int]
calculate = [ calculate (x+candidate) | x <- coins, x > candidate]
Run Code Online (Sandbox Code Playgroud)

我已经阅读了一些教程,结果还可以.我正在尝试解决一些小问题,让我对语言有所了解.但是我坚持这个.

test.hs:3:0: parse error (possibly incorrect indentation)
Run Code Online (Sandbox Code Playgroud)

谁能告诉我为什么?我今天开始使用haskell,所以请轻松解释.

我试过像它一样运行它:

runghc test.hs
ghc test.hs
Run Code Online (Sandbox Code Playgroud)

但是:

ghci < test.hs
Run Code Online (Sandbox Code Playgroud)

它给出了这个:

<interactive>:1:10: parse error on input `='
Run Code Online (Sandbox Code Playgroud)

谢谢

Tho*_*son 5

1)顶级声明不需要'let'.你可能想要:

coins = [50, 25, 10, 5, 2,1]

candidate = 11
Run Code Online (Sandbox Code Playgroud)

2)计算明确地作为列表键入并用作函数.

这里是你说calculate是一个整数列表:

calculate :: [Int]
Run Code Online (Sandbox Code Playgroud)

在你使用的列表理解中calculate (x+candidate),你已经明确地计算了一个列表而不是一个函数 - 所以这不起作用.

calculate = [ calculate (x+candidate) | x <- coins, x > candidate]
Run Code Online (Sandbox Code Playgroud)

也许你想要的东西:

newCoins = [ x + candidate | x <- coins, x > candidate]
Run Code Online (Sandbox Code Playgroud)

如果您在结果中解释了更多您想要的内容,那将会有所帮助.