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

Alex
  • 8,093
  • 6
  • 49
  • 79
Son
  • 1,835
  • 2
  • 18
  • 28

4 Answers4

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
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 15
    Thanks. "Real world haskell" (at least the version i have) doesn't have the `let` in its examples – Micah Jul 19 '12 at 21:48
  • 43
    ["Learn you Haskell"](http://learnyouahaskell.com/starting-out) doesn't mention this at all. – Bakuriu Jun 01 '13 at 07:40
  • 2
    @Bakuriu `LYAH` [does](http://learnyouahaskell.com/starting-out#an-intro-to-lists) 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? – isomorphismes Aug 05 '13 at 15:33
  • 9
    @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. – superzamp May 10 '14 at 10:31
  • This tutorial is then plain wrong: http://www.seas.upenn.edu/~cis194/lectures/01-intro.html . Yet it is the first tutorial recommended on the haskell website! – cammil Aug 21 '15 at 09:28
  • @superzamp Yes, LYAH's author mentions (very much in passing) to load from a file, then immediately proceeds to using the prompt for several sections, only to then (at "Pattern Matching") suddenly revert to file-loading mode without any kind of warning. This could definitely improve. – user118967 Feb 12 '16 at 20:37
  • Thanks a lot for this :) – Pragyaditya Das Aug 26 '16 at 18:25
  • This answer is obsolete. See glguy's answer below. Works in GHCi 8.0.1. – MilesMcBain Jan 11 '17 at 12:06
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
dave4420
  • 46,404
  • 6
  • 118
  • 152
  • 12
    Why doesn't it work in GHCi? Why is there a difference in the syntax? – Beat Apr 11 '15 at 13:30
  • 1
    @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. – AJF Apr 14 '17 at 08:56
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.

Community
  • 1
  • 1
Raeez
  • 2,423
  • 15
  • 12
  • 27
    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 Jan 27 '13 at 19:09
  • 3
    @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. – Vietnhi Phuvan Apr 05 '14 at 13:49
  • 10
    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. – Brian Tingle Oct 18 '14 at 14:49
4

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

glguy
  • 1,090
  • 7
  • 8