【发布时间】:2015-08-22 02:40:08
【问题描述】:
我在 Windows 7 上运行 GHC 版本 7.8.3。
好的,这与花哨的代码 sn-ps 无关。我只是不想在这里成为菜鸟,而是以一种与副作用语言的结构有点相似的方式实际编译一些东西。
我有以下代码:
main =
do {
let x = [0..10];
print x
}
I've learned here,关键字 do 是花哨的单子表达式的花哨语法糖。当我尝试编译它时,我收到以下错误:
main.hs:4:1: parse error on input 'print'
我在this other question 中了解到,Haskell 中的标签是邪恶的,所以我试图省略它们:
main =
do {
let x = [0..10];
print x
}
而且我失败得很惨,因为解析错误仍然存在。
I've also learned here,那个 print 是花哨的等价物的语法糖:
main =
do {
let x = [0..10];
putStrLn $ show x
}
但后来我得到了这个错误:
main.hs:4:9: parse error on input 'putStrLn'
试图面对我的绝望,我试图省略 let 关键字,after reading this answer:
main =
do {
x = [0..10];
print x
}
然后我得到:
main.hs:4:1: parse error on input '='
在最后一次无用的尝试中,我什至试图省略';'像这样:
main =
do {
let x = [0..10]
print x
}
得到:
main.hs:4:1: parse error on input 'print'
所以,
如何在 Haskell 中正确使用一元表达式而不会出现解析错误?有希望吗?
【问题讨论】:
-
如果您使用显式大括号,解析器似乎需要在 let 语句中使用
in。所以do { let x = 10 ; print x }不起作用,但do { let x = 10 in print x }起作用。或者,省略大括号,您可以省略in。