如何在 GHCi 中使用多个 where 子句?
我是第一次使用 GHCi,在编写多行函数时遇到了一些问题。 我的代码如下:
Prelude> :{
Prelude| let diffSquares lst = abs $ squareOfSums lst - sumOfSquares lst
Prelude| where
Prelude| squareOfSums lst = (fst (sumsAndSquares lst))^2
Prelude| sumOfSquares lst = snd (sumsAndSquares lst)
Prelude| sumsAndSquares = foldl (\(sms,sqrs) x -> (sms+x,sqrs+x^2)) (0,0)
Prelude| :}
它给出了以下错误:
<interactive>:1:142: parse error on input `='
有人可以指出我所缺少的方向吗?
I'm playing around with GHCi for the first time, and I'm having some trouble writing multi-line functions.
My code is as follows:
Prelude> :{
Prelude| let diffSquares lst = abs $ squareOfSums lst - sumOfSquares lst
Prelude| where
Prelude| squareOfSums lst = (fst (sumsAndSquares lst))^2
Prelude| sumOfSquares lst = snd (sumsAndSquares lst)
Prelude| sumsAndSquares = foldl (\(sms,sqrs) x -> (sms+x,sqrs+x^2)) (0,0)
Prelude| :}
It gives the following error:
<interactive>:1:142: parse error on input `='
Could someone kindly point me in the direction of what I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 ghci 的帮助手册 (http:// www.haskell.org/ghc/docs/6.10.4/html/users_guide/interactive-evaluation.html):
因此,您必须在每个定义之间插入分号,例如
编辑:在最新版本的 GHCi 中,您似乎需要一对大括号。
From the help manual of ghci (http://www.haskell.org/ghc/docs/6.10.4/html/users_guide/interactive-evaluation.html):
Therefore you must insert a semicolon between each definition, e.g.
Edit: It seems you need a pair of braces instead in the recent version of GHCi.
缩进的黄金法则:作为某些表达式一部分的代码应该比该表达式的开头进一步缩进(即使该表达式不是该行的最左边元素)。
错误:
正确:
不需要大括号或分号。
The golden rule of indentation: code which is part of some expression should be indented further in than the beginning of that expression (even if the expression is not the leftmost element of the line).
Wrong:
Right:
No need for braces or semicolons.