在 haskell 中工作,发现奇怪的行为,将其精简为简单的框架
这有效
a :: Bool
a = case True of
True -> True
False -> False
但当我尝试时
b :: IO Bool
b = do
let b' = case True of
True -> True
False -> False
return b'
I get
ghci>:l test.hs
[1 of 1] Compiling Main ( test.hs, interpreted )
test.hs:16:14: parse error on input ‘->’
Failed, modules loaded: none.
So I try
c :: IO Bool
c = do
let c' = case True of
True -> True
False -> False
return c'
这有效。
什么?为什么?在这种情况下为什么需要额外的缩进?我找不到任何相关内容,可能是因为这些关键字在日常语言中非常短且常见。是否有一些规范可以解释这种行为?
基本的缩进规则实际上非常简单:
- 在开始块的关键字之后(
where
,let
,do
,case .. of
)记下下一个单词开始的列(可能在下一行)
- 行的缩进与块中的新条目完全相同
- 缩进多于前一个条目的行
- 缩进小于该行之前的块的行结束
- 在嵌套块中,首先将规则应用于最外面的块
棘手的例子:
1 + case x of
A -> 45 -- note where "A" starts
B -> 10 -- same indentation: another case branch
+ 2 -- more indented, so it's "10+2"
+ 10 -- less indented, so it's "1+(case ...)+10"
就你而言,
let b' = case True of
True -> True
False -> False
我们有两个嵌套块,一个用于let
和一个用于case..of
. The let
块使用的列b'
. The case..of
块尝试重用同一列,但我们需要首先将规则应用于最外层的块。所以True -> ...
该行实际上是一个新条目let
堵塞。这会触发解析错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)