如何在 GHCi 中使用多个 where 子句?

发布于 2024-09-06 06:58:01 字数 506 浏览 1 评论 0原文

我是第一次使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来自 ghci 的帮助手册 (http:// www.haskell.org/ghc/docs/6.10.4/html/users_guide/interactive-evaluation.html):

此类多行命令可以与任何 GHCi 命令一起使用,并且 :{:} 之间的行简单地合并为单行以进行解释< /强>。这意味着每个这样的组在合并时必须形成一个有效的命令,并且不使用布局规则


因此,您必须在每个定义之间插入分号,例如

Prelude> :{
Prelude| let a x = g
Prelude|   where
Prelude|     g = p x x;      {- # <----- # -}
Prelude|     p a b = a + b
Prelude| :}

编辑:在最新版本的 GHCi 中,您似乎需要一对大括号。

Prelude> :{
Prelude| let { a x = g
Prelude|   where
Prelude|     g = p x x
Prelude|     p a b = a + b
Prelude| }
Prelude| :}
Prelude> a 5
10

From the help manual of ghci (http://www.haskell.org/ghc/docs/6.10.4/html/users_guide/interactive-evaluation.html):

Such multiline commands can be used with any GHCi command, and the lines between :{ and :} are simply merged into a single line for interpretation. That implies that each such group must form a single valid command when merged, and that no layout rule is used.

Therefore you must insert a semicolon between each definition, e.g.

Prelude> :{
Prelude| let a x = g
Prelude|   where
Prelude|     g = p x x;      {- # <----- # -}
Prelude|     p a b = a + b
Prelude| :}

Edit: It seems you need a pair of braces instead in the recent version of GHCi.

Prelude> :{
Prelude| let { a x = g
Prelude|   where
Prelude|     g = p x x
Prelude|     p a b = a + b
Prelude| }
Prelude| :}
Prelude> a 5
10
悲欢浪云 2024-09-13 06:58:01

缩进的黄金法则作为某些表达式一部分的代码应该比该表达式的开头进一步缩进(即使该表达式不是该行的最左边元素)。

Prelude> :set +m

错误:

Prelude> let foo = x
Prelude|     where x = 1
Prelude| 

<interactive>:3:1:
    parse error in let binding: missing required 'in'

正确:

Prelude> let foo = x
Prelude|      where x = 1
Prelude| 

不需要大括号或分号。

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).

Prelude> :set +m

Wrong:

Prelude> let foo = x
Prelude|     where x = 1
Prelude| 

<interactive>:3:1:
    parse error in let binding: missing required 'in'

Right:

Prelude> let foo = x
Prelude|      where x = 1
Prelude| 

No need for braces or semicolons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文