Haskell 错误:输入 `=' 时出现解析错误
规格
GHC 6.12.1
Mac OS X 10.6.4 x64
MacBook Pro
问题
我在使用 let
语法时遇到问题。以下代码拒绝编译:
module Main where
main = let x = 1
y = 2
z = 3
in putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z
我尝试在 y = 2
和 z = 3
中进行更多操作。没有骰子。
(不需要的)解决方案
我编译代码的唯一方法是
- 用空格替换硬制表符。
- 将
let
子句替换为where
子句。
Specs
GHC 6.12.1
Mac OS X 10.6.4 x64
MacBook Pro
Problem
I'm having trouble using let
syntax. The following code refuses to compile:
module Main where
main = let x = 1
y = 2
z = 3
in putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z
I tried tabbing in y = 2
and z = 3
even more. No dice.
(Undesirable) Solutions
The only way I've gotten the code to compile is either
- Replacing hard tabs with spaces.
- Replacing the
let
clause with awhere
clause.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
#haskell 上的 Saizan 解释说,let 表达式中的赋值必须对齐,而不是 let 本身。只要分配一致,就可以使用硬选项卡或软选项卡。
正确代码:
Saizan on #haskell explains that the assignments in a let expression have to align, not let itself. As long as the assignments line up, it's okay to use hard tabs or soft tabs.
Correct code:
您根本无法使用制表符正确控制缩进,因为制表符的大小未定义。
因此,不要在 Haskell 中使用制表符。他们是邪恶的。
You simply can't control indentation correctly with tabs because the size of a tab is undefined.
Therefore, don't use tabs in Haskell. They're evil.
将 let 块中的每个声明缩进相同的程度。同样好的形式是将“in”和“let”缩进到同一水平。例如..
Indent each declaration in the let-block to the same degree. Also good form is to indent the 'in' and 'let' to the same level. Eg..
就我个人而言,我在每行末尾添加分号
Personally, I put semicolon at the end of each line
如果您坚持在源代码中使用制表符,则将进行以下编译:
其中所有前导空格都是一个或两个制表符,并且
let
和x = 1
之间的空格也是一个选项卡。在 vi 的 列表模式 中查看,使 TAB 和行尾显式显示:如果您切换到空格,您的生活会变得更加简单,您的代码也会更加漂亮。
If you insist on TAB characters in your source, the following compiles:
where all leading whitespace is either one or two TABs, and the whitespace between
let
andx = 1
is also a TAB. Viewed in vi's list mode to make TABs and line-ends explicit:Your life will be much simpler and your code prettier if you switch to spaces.