let语句中的case语句需要什么缩进? [英] What indentation is required for a case statement within a let statement?

查看:100
本文介绍了let语句中的case语句需要什么缩进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在haskell工作,发现了奇怪的行为,将其剥离为裸露的骨骼



此作品

  a :: Bool 
a = case
真正 - > True
False - >假

但是当我尝试时

  b :: IO Bool 
b = do
let b'=大小写真
True - > True
False - > False
return b'

我得到

  ghci>:l test.hs 
[1 of 1]编译Main(test.hs,解释)

test.hs: 16:14:解析输入错误' - >'
失败,模块加载:无。

所以我尝试

  c :: IO Bool 
c = do
let c'= case真
真 - > True
False - > False
return c'

这是有效的。



什么?为什么?为什么在这种情况下需要额外缩进?我找不到任何东西,可能是因为这些关键字在日常语言中非常短暂和常见。是否有一些规范解释了这种行为?

解决方案

我没有准确的措辞,但是 Wikibook页面相当清楚地解释了这个问题。



<为什么它的工作原理很简单:支持通过一个let-group绑定多个变量,比如:

  c = do 
让c'= ...
d = ...
e = ...
return c'

您的 True - > ... False - > ... 被错误地解释为要绑定的其他变量。


Working in haskell, found odd behavior, stripped it down to bare bones

This Works

a :: Bool
a = case True of
    True -> True
    False -> False

But when I try

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'

And this works.

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?

解决方案

I don't have the exact wording from the spec, but this Wikibook page explains the issue quite clearly.

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'

Your True -> … and False -> … are mistakenly interpreted as additional variables to be bound.

这篇关于let语句中的case语句需要什么缩进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆