Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "parse error on input" in haskell?

Tags:

haskell

Prelude Data.Set> :load hello
[1 of 1] Compiling Main             ( hello.hs, interpreted )

hello.hs:11:11: parse error on input `<-'
Failed, modules loaded: none.
Prelude Data.Set> h <- IO.openFile "testtext" IO.ReadMode
Prelude Data.Set> 

The same line [h <- IO.openFile "testtext" IO.ReadMode] inside hello.hs throws the error. How do i fix this? What am I doing wrong?

[EDIT] Source and output: http://pastebin.com/KvEvggQK

like image 885
Karthick Avatar asked Dec 19 '10 09:12

Karthick


People also ask

What does parse error on input mean in Haskell?

tmr. hs:1:14: parse error on input '->' Failed, modules loaded: none. Parse errors indicate that the program violates Haskell syntax.

What does parsing error mean?

A parse error is an error message you sometimes get on Android devices when an app fails to install. The message itself is not very specific, and there are a lot of problems that can cause it.


1 Answers

You can only use <- inside a do-block¹ (which you're implicitly in in GHCI, but not in Haskell files).

In a Haskell file, you're only allowed to write bindings using =.

What you could do is put the following in the Haskell file:

myHandle = do h <- IO.openFile "testtext" IO.ReadMode
              return h

Though if you think about that for a bit, this is just the same as:

myHandle = IO.openFile "testtext" IO.ReadMode

However this way myHandle is still wrapped in IO and you'll need <- (or >>=) in ghci to unwrap it.

You can't write a Haskell file in such a way that just loading the file, will open testtext and give you the file handle.


¹ Or a list comprehension, but there the right operand of <- needs to be a list, so that has nothing to do with your situation.

like image 66
sepp2k Avatar answered Sep 20 '22 22:09

sepp2k