116

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

4 Answers 4

161

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
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. "Real world haskell" (at least the version i have) doesn't have the let in its examples
"Learn you Haskell" doesn't mention this at all.
@Bakuriu LYAH does now mention let. But a follow-up. In LYAH I read addThree :: Int -> Int -> Int -> Int (newline) addThree x y z = x + y + z but only the second one runs in GHCi with let. Why?
@Bakuriu Yes but the author tell you to write your definitions in an external file and load it within GHCI, not to write them directly in GHCI. And the former works perfectly.
This tutorial is then plain wrong: seas.upenn.edu/~cis194/lectures/01-intro.html . Yet it is the first tutorial recommended on the haskell website!
|
50

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

2 Comments

Why doesn't it work in GHCi? Why is there a difference in the syntax?
@Beat GHCi tries to evaluate expressions by default, not parse statements, whereas the file format is the opposite. That's why, to make statements (ie: set variables, define functions, etc) you have to declare that you're doing using let. Think of GHCi as one big let ... in ... statement.
21

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).

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

3 Comments

This answer is useless for a beginner. He's looking for a simple actionable hint to move forward, not advanced topics. You don't explain polynomial products to a kid learning the multiplication table -- it doesn't show how much you know, it shows you don't know how to share what you do know.
@btk: everyone has to stop being a beginner at some point. I started learning Haskell yesterday and I am confident that within a short time, I will understand everything Raeez says.
This is my first day learning Haskell, and I found this answer very helpful for understanding why I have to use let; I was like "wtf, why do I have to use let" and then I read this and was enlightened.
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.