gpt4 book ai didi

Haskell 简单编译错误

转载 作者:行者123 更新时间:2023-12-02 16:46:48 24 4
gpt4 key购买 nike

我正在尝试运行此代码:

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

let candidate = 11

calculate :: [Int]
calculate = [ calculate (x+candidate) | x <- coins, x > candidate]

我读过一些教程,效果还不错。我正在尝试解决一些小问题,以便让我感受到这门语言。但我被困在这个问题上。

test.hs:3:0: parse error (possibly incorrect indentation)

谁能告诉我为什么?我今天开始使用 haskell,所以请简单地解释一下。

我尝试像这样运行它:

runghc test.hs
ghc test.hs

但是:

ghci < test.hs

它给出了这个:

<interactive>:1:10: parse error on input `='

谢谢

最佳答案

1) 顶级声明不需要“let”。您可能想要:

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

candidate = 11

2)Calculate 明确键入为列表并用作函数。

这里是你所说的计算是整数列表的地方:

calculate :: [Int]

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

calculate = [ calculate (x+candidate) | x <- coins, x > candidate]

也许您想要类似的东西:

newCoins = [ x + candidate | x <- coins, x > candidate]

如果您能更多地解释您想要的结果,将会有所帮助。

关于Haskell 简单编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2404937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com