1

有人告诉我,你可以通过正确使用 { ... ; 使 Haskell 空格不敏感。... ;} 到目前为止,除了 where 子句之外,我大部分时间都能做到这一点。有效的示例代码:

primes = 2 : sieve [3,5..] 
    where {sieve = ... };

就像这样:

primes = 2 : sieve [3,5..] where {sieve = ...};

然而,这是一个解析错误:

primes = 2 : sieve [3,5..]
where {sieve = ...};

知道如何使 where 子句空格的开头不敏感吗?

4

2 回答 2

4

这不起作用,因为您的缩进where使 Haskell thinkprimes的定义已经结束。然后它会感到困惑,因为有一个where子句悬在顶层。

您可以在某些关键字之后使用块,{}let, (如)和. 所以在你的情况下,你只需要省略换行符。whereofcase foo ofdo

primes = 2:sieve [3,5..] where {
sieve = ....
}
primes' = 2:sieve [3,5..] where
{sieve = ...}

如果您真的想在任何地方使用它,则还必须使用显式模块声明

module Main where {
;primes' = 2:sieve [3,5..]
where {sieve = ...}
}

我建议不要这样做,而只是使用缩进。Haskell 中的显式大括号通常用于生成的代码,当您看到它们是手写的时,几乎总是带有do.

于 2013-05-31T20:06:06.520 回答
2

查看haskell 报告的 lexemes 部分的末尾,了解使用大括号和分号的示例。

这是我要编译的一个简单程序:

module Temp where {
foo = 1           
where x = 2        
}                  

请注意模块级大括号的必要性,由于报告的这一部分:

在这些显式的左大括号内,不会对大括号外的结构执行布局处理,即使一行缩进到较早的隐式左大括号的左侧也是如此。

于 2013-05-31T20:27:40.180 回答