Haskell 中 do 块中的简洁 if-then-else 表示法
我不知道如何使简洁的 if-then-else 表示法起作用,在 [ http://hackage.haskell.org/trac/haskell-prime/wiki/DoAndIfThenElse]。这可行,
import System.Environment
main = do
args <- getArgs
if (args !! 0) == "hello"
then
print "hello"
else
print "goodbye"
但事实并非如此,插入所述分号(请参阅链接)只会导致我的解析错误。
import System.Environment
main = do
args <- getArgs
if (args !! 0) == "hello" then
print "hello"
else
print "goodbye"
I cannot figure out how to make the concise if-then-else notation work, mentioned at [ http://hackage.haskell.org/trac/haskell-prime/wiki/DoAndIfThenElse ]. This works,
import System.Environment
main = do
args <- getArgs
if (args !! 0) == "hello"
then
print "hello"
else
print "goodbye"
but this does not, and inserting said semicolons (see link) just result in parse errors for me.
import System.Environment
main = do
args <- getArgs
if (args !! 0) == "hello" then
print "hello"
else
print "goodbye"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您提供的链接描述了一个提案,听起来它不是 Haskell 标准的一部分(尽管该链接提到它是在 jhc、GHC 和 Hugs 中实现的)。您正在使用的 Haskell 编译器版本或您正在使用的标志集可能不允许链接中描述的可选分号行为。
试试这个:
The link you provided describes a proposal, which sounds like it is not part of the Haskell standard (although the link mentions that it's implemented in jhc, GHC and Hugs). It's possible that the version of the Haskell compiler you're using, or the set of flags you're using, does not allow for the optional-semicolon behavior described in the link.
Try this:
在 Haskell 98 中,“if … then … else …”是单个表达式。如果它被分成多行,则第一行后面的行必须进一步缩进。
就像以下是错误的......
而以下有效......
以下也是错误的......
并且以下有效。
正如其他评论已经提到的, Haskell 2010 允许“ then”和“else”部分与“if”部分处于同一缩进级别。
In Haskell 98 “if … then … else …” is a single expression. If it’s split to multiple lines, the ones following the first one must be indented further.
Just like the following is wrong…
…and the following works…
…the following is also wrong…
…and the following works.
As the other comments already mention, Haskell 2010 allows the “then” and “else” parts on the same level of indentation as the “if” part.
Haskell 语法和语言通过 源文件。 DoAndIfThenElse 扩展被识别,因为它是 Cabal 文档。当前的 GHC 默认启用此功能。
Haskell syntax and language are extended though {-# LANGUAGE ... #-} pragmas at the start of the source files. The DoAndIfThenElse extension is recognized since it is one of those listed in the Cabal documentation. Current GHC enables this by default.
我通常将
else
比if
多缩进一个空格。除非整个if
非常适合单行。I usually indent the
else
one space more than theif
. Unless then wholeif
fits nicely on a single line.