如何在Haskell中嵌套let语句? [英] How to nest let statements in Haskell?

查看:138
本文介绍了如何在Haskell中嵌套let语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图嵌套一对let语句,但是我得到的语法错误对我来说没有意义。我对Haskell编程非常陌生,所以我确定这是我不明白的东西(可能与间距有关)。我明白,让和必须在同一列。

I'm trying to nest a couple let statements, but I'm getting syntax errors that don't make sense to me. I'm really new to Haskell programming so I'm sure it's something I just don't understand (probably having to do with the spacing). I understand that let and in must be in the same column.

为什么会这样:

aaa = let y = 1+2
          z = 4+6
      in y+z

完美地工作,而

Works perfectly fine, whereas

aaa = let y = 1+2
          z = 4+6
          in let f = 3
                 e = 3
             in e+f

给了我错误:表达式中的语法错误(意外的`=')

gives me the error: "Syntax error in expression (unexpected `=')"

推荐答案

第二个例子 z = ... y = ... 不一致。在> 块中,每个定义都必须对齐。

In the second example, the z = ... isn't aligned with the y = .... In a let block, every definition has to be aligned.

我怀疑你用制表符缩进,并让您的编辑器将标签显示为少于8个空格,使其看起来像与您对齐。你应该用空格来替换标签,并且最好设置你的编辑器将标签扩展为空格,以避免将来出现这样的问题。

I suspect you're indenting with tab characters, and have your editor set to display tabs as less than 8 spaces, making it look like it's aligned to you. You should replace the tab with spaces, and preferably set your editor to expand tabs into spaces to avoid problems like this in the future.

这篇关于如何在Haskell中嵌套let语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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