let 语句中的 case 语句需要什么缩进? [英] What indentation is required for a case statement within a let statement?
问题描述
在 Haskell 中工作,发现奇怪的行为,将其剥离为裸露的骨头
Working in haskell, found odd behavior, stripped it down to bare bones
这个作品
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'
我明白
ghci>:l test.hs
[1 of 1] Compiling Main ( test.hs, interpreted )
test.hs:16:14: parse error on input ‘->’
Failed, modules loaded: none.
所以我试试
c :: IO Bool
c = do
let c' = case True of
True -> True
False -> False
return c'
这行得通.
什么?为什么?为什么在这种情况下我需要额外的缩进?我找不到任何关于此的信息,可能是因为这些关键字在日常语言中是如此简短和常见.是否有一些规范可以解释这种行为?
What? Why? Why do I need an extra indent in this case? I can't find anything on this, probably because these keyword are so short and common in everyday language. Is there some spec that explains this behavior?
推荐答案
我没有规范中的确切措辞,但是这个 Wikibook page 非常清楚地解释了这个问题.
I don't have the exact wording from the spec, but this Wikibook page explains the issue quite clearly.
这样做的原因很简单:支持通过单个let-group绑定多个变量,例如:
The reason why it works like this is simple: to support binding multiple variables via a single let-group, such as:
c = do
let c' = …
d = …
e = …
return c'
你的 True ->...
和 False ->...
被错误地解释为要绑定的附加变量.
Your True -> …
and False -> …
are mistakenly interpreted as additional variables to be bound.
这篇关于let 语句中的 case 语句需要什么缩进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!