输入' ='时Haskell错误解析错误

时间:2022-02-26 17:22:52

I'm new to Haskell and after starting ghci I tried:

我是Haskell的新手,在开始ghci之后我尝试:

f x = 2 * x

and I obtained:

我获得:

<interactive>:1:4: parse error on input `='

which I don't understand.

我不明白你的意思。

Strangely, it worked well before. I suppose that I have done misconfigured Haskell. Reinstalling ghc6 doesn't solve the problem.

奇怪的是,它以前运行得很好。我想我做错了Haskell配置。重新安装ghc6并不能解决这个问题。

For information, I use Ubuntu 10.4 and the version of ghc6 is 6.12.1-12

对于信息,我使用Ubuntu 10.4,而ghc6的版本是6.12.1-12。

4 个解决方案

#1


155  

In GHCi 7.x or below, you need a let to define things in it.

GHCi 7。x或以下,你需要一个let来定义里面的东西。

Prelude> let f x = x * 2
Prelude> f 4
8

Starting from GHC 8.0.1, top-level bindings are supported in GHCi, so OP's code will work without change.

从GHC 8.0.1开始,在GHCi中支持*绑定,因此OP的代码将不会改变。

GHCi, version 8.0.1.20161213: http://www.haskell.org/ghc/  :? for help
Prelude> f x = x * 2
Prelude> f 4
8

#2


50  

When you type into a Haskell source file,

当您输入Haskell源文件时,

f x = 2 * x

is correct.

是正确的。

When you type directly into ghci, you need to type let at the start of the line:

当您直接输入到ghci时,您需要在行首输入let:

let f x = 2 * x

#3


20  

A good rule of thumb for using ghci is that any code you enter should conform to do-block semantics; that is, you could assume syntactically that you're programming within the IO monad (if this is new terminology, don't worry! I'd highly recommend reading through this tutorial).

使用ghci的一个很好的经验法则是,您输入的任何代码都应该符合do-block语义;也就是说,您可以从语法上假定您正在IO monad中进行编程(如果这是新的术语,不要担心!我强烈推荐阅读本教程。

This answer illustrates this point with an example, and may provide more working insight into the nature of IO and ghci.

这个答案通过一个示例说明了这一点,并可能提供对IO和ghci的本质的更有效的理解。

#4


4  

Starting in GHC 8.0.1 this would no longer generate an error.

从GHC 8.0.1开始,这将不再产生错误。

#1


155  

In GHCi 7.x or below, you need a let to define things in it.

GHCi 7。x或以下,你需要一个let来定义里面的东西。

Prelude> let f x = x * 2
Prelude> f 4
8

Starting from GHC 8.0.1, top-level bindings are supported in GHCi, so OP's code will work without change.

从GHC 8.0.1开始,在GHCi中支持*绑定,因此OP的代码将不会改变。

GHCi, version 8.0.1.20161213: http://www.haskell.org/ghc/  :? for help
Prelude> f x = x * 2
Prelude> f 4
8

#2


50  

When you type into a Haskell source file,

当您输入Haskell源文件时,

f x = 2 * x

is correct.

是正确的。

When you type directly into ghci, you need to type let at the start of the line:

当您直接输入到ghci时,您需要在行首输入let:

let f x = 2 * x

#3


20  

A good rule of thumb for using ghci is that any code you enter should conform to do-block semantics; that is, you could assume syntactically that you're programming within the IO monad (if this is new terminology, don't worry! I'd highly recommend reading through this tutorial).

使用ghci的一个很好的经验法则是,您输入的任何代码都应该符合do-block语义;也就是说,您可以从语法上假定您正在IO monad中进行编程(如果这是新的术语,不要担心!我强烈推荐阅读本教程。

This answer illustrates this point with an example, and may provide more working insight into the nature of IO and ghci.

这个答案通过一个示例说明了这一点,并可能提供对IO和ghci的本质的更有效的理解。

#4


4  

Starting in GHC 8.0.1 this would no longer generate an error.

从GHC 8.0.1开始,这将不再产生错误。