How to nest let statements in Haskell?

♀尐吖头ヾ 提交于 2019-12-10 12:37:24

问题


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.

Why is it that:

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 `=')"


回答1:


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

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.



来源:https://stackoverflow.com/questions/10148642/how-to-nest-let-statements-in-haskell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!