8

I'm going through the book Natural Language Processing for Working Programmers. The book uses Haskell, which I don't have much experience with. The code below throws an error in GHCI

:{
do
  l <- [0..9]
  ps <- (\x -> [x-1, x+2]) l
  return ps
:}

This is the error message returned

The last statement in a 'do' construct must be an expression

All answers I've come across seem to suggest it is an indentation error, but as far as I can tell the indentation seems correct. Any ideas what the problem could be?

Okal Otieno
  • 345
  • 4
  • 13

2 Answers2

4

I typed your code into ghci 7.0.3 and did not get an error.

Prelude> :{
Prelude| do
Prelude|   l <- [0..9]
Prelude|   ps <- (\x -> [x-1, x+2]) l
Prelude|   return ps
Prelude| :}
[-1,2,0,3,1,4,2,5,3,6,4,7,5,8,6,9,7,10,8,11]

Edit: When I use ghci 6.12.1 as packaged in Ubuntu 10.04, I get the same error as you.

dave4420
  • 46,404
  • 6
  • 118
  • 152
0

The symbols of :{ and :} are not part of Haskell, I think that is something to do with the text you are reading. Also, the code you posted has a lambda being used as a list monad. Try this:

do
  l <- [0..9]
  ps <- (\x -> [x-1, x+2]) l
  return ps
Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166
  • 1
    I was under the impression the `:{` and `:}` were necessary for multiline code in GHCi. Left out the list argument by mistake, sorry. I haven't quite wrapped my head around monads though. Am I supposed to use your code as is in GHCi? Each line is being interpreted independently. – Okal Otieno Dec 29 '11 at 06:02
  • I think I may have found a way out, based on http://stackoverflow.com/a/3532505/420386. Using semi-colons seems to solve the problem, although it makes me feel a little dirty :) Thanks. – Okal Otieno Dec 29 '11 at 07:04
  • Sorry, I somehow missed you were in GHCi (and forgot about that syntax). – Thomas M. DuBuisson Dec 29 '11 at 15:34