Haskell 错误:输入 `=' 时出现解析错误

发布于 2024-09-13 14:01:25 字数 555 浏览 4 评论 0原文

规格

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 = 2z = 3 中进行更多操作。没有骰子。

(不需要的)解决方案

我编译代码的唯一方法是

  1. 用空格替换硬制表符。
  2. 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

  1. Replacing hard tabs with spaces.
  2. Replacing the let clause with a where clause.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟若柳尘 2024-09-20 14:01:25

#haskell 上的 Saizan 解释说,let 表达式中的赋值必须对齐,而不是 let 本身。只要分配一致,就可以使用硬选项卡或软选项卡。

正确代码:

module Main where

main = let
        x = 1
        y = 2
        z = 3
    in putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z

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:

module Main where

main = let
        x = 1
        y = 2
        z = 3
    in putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z
黒涩兲箜 2024-09-20 14:01:25

您根本无法使用制表符正确控制缩进,因为制表符的大小未定义。

因此,不要在 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.

酸甜透明夹心 2024-09-20 14:01:25

将 let 块中的每个声明缩进相同的程度。同样好的形式是将“in”和“let”缩进到同一水平。例如..

main = let x = 1
           y = 2
           z = 3
       in putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z

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..

main = let x = 1
           y = 2
           z = 3
       in putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z
宫墨修音 2024-09-20 14:01:25

就我个人而言,我在每行末尾添加分号

module Main where

main = let x = 1 ;
           y = 2 ;
           z = 3 
in putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z

Personally, I put semicolon at the end of each line

module Main where

main = let x = 1 ;
           y = 2 ;
           z = 3 
in putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z
故人的歌 2024-09-20 14:01:25

如果您坚持在源代码中使用制表符,则将进行以下编译:

module Main where

main =
    let x = 1
        y = 2
        z = 3
    in putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z

其中所有前导空格都是一个或两个制表符,并且 letx = 1 之间的空格也是一个选项卡。在 vi 的 列表模式 中查看,使 TAB 和行尾显式显示:

module Main where$
$
main =$
^Ilet^Ix = 1$
^I^Iy = 2$
^I^Iz = 3$
^Iin putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z$

如果您切换到空格,您的生活会变得更加简单,您的代码也会更加漂亮。

If you insist on TAB characters in your source, the following compiles:

module Main where

main =
    let x = 1
        y = 2
        z = 3
    in putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z

where all leading whitespace is either one or two TABs, and the whitespace between let and x = 1 is also a TAB. Viewed in vi's list mode to make TABs and line-ends explicit:

module Main where$
$
main =$
^Ilet^Ix = 1$
^I^Iy = 2$
^I^Iz = 3$
^Iin putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z$

Your life will be much simpler and your code prettier if you switch to spaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文