我是新来的哈斯克尔,并得到这个臭名昭著的错误。
我已经查阅了以下链接:Haskell: parse error on input `|' Haskell parse error on input '|' Haskell - parse error on input `|' Why complains Haskell parse error on input `|' in this Function? Haskell parse error on input `|'
真正令我惊讶的是,我准确地抄袭了,也就是我的大学老师在课堂上给我们的代码:
data TreeInt = Leaf Int
| Node TreeInt Int TreeInt
foo :: TreeInt -> Int
foo arg =
case arg of
| Leaf x = x
| Node tLeft x tRight = x我知道问题在foo arg下面,因为下面的代码编译:
data TreeInt = Leaf Int
| Node TreeInt Int TreeInt
foo :: TreeInt -> Int
foo arg = undefined准确的错误是:hw.hs:6:4: error: parse error on input ‘|’,这让我相信它在第6行({##**$$}})。
我试过的是:
发布于 2017-05-23 00:08:01
而不是这个(#1):
case arg of
| Leaf x = x
| Node tLeft x tRight = x你想要这个(#2):
case arg of
Leaf x -> x
Node tLeft x tRight -> x#1的样式在其他ML-家庭语言中使用,例如OCaml:
match arg with
| Leaf x -> x
| Node (tLeft, x, tRight) -> x但是Haskell使用“布局规则”将desugar #2转换为以下内容:
case arg of {
Leaf x -> x;
Node tLeft x tRight -> x;
}(事实上,如果您愿意,可以显式地编写它。)
还请注意,->用于case表达式,=用于定义:
foo arg =
case arg of
Leaf x -> x
Node tLeft x tRight -> x
foo' (Leaf x) = x
foo' (Node tLeft x tRight) = x即使在模式上有一个保护表达式,这也是正确的--垂直条形图(|)实际上用于:
foo arg =
case arg of
Leaf x
| x < 0 -> 0
| otherwise -> x
Node tLeft x tRight
| x < 0 -> 0
| otherwise -> x
foo' (Leaf x)
| x < 0 = 0
| otherwise = x
foo' (Node tLeft x tRight)
| x < 0 = 0
| otherwise = xhttps://stackoverflow.com/questions/44123811
复制相似问题