Haskell error parse error on input `='

Haskell

Haskell Problem Overview


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

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.

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

Haskell Solutions


Solution 1 - Haskell

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

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.

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

Solution 2 - Haskell

When you type into a Haskell source file,

f x = 2 * x

is correct.

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

let f x = 2 * x

Solution 3 - Haskell

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 http://www.haskell.org/haskellwiki/Introduction_to_Haskell_IO/Actions">tutorial</a>;).

https://stackoverflow.com/questions/3126591/why-cant-i-define-a-new-type-in-ghci/3129206#3129206">This</a> answer illustrates this point with an example, and may provide more working insight into the nature of IO and ghci.

Solution 4 - Haskell

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSonView Question on Stackoverflow
Solution 1 - HaskellkennytmView Answer on Stackoverflow
Solution 2 - Haskelldave4420View Answer on Stackoverflow
Solution 3 - HaskellRaeezView Answer on Stackoverflow
Solution 4 - HaskellglguyView Answer on Stackoverflow