I am an absolute beginner. Going through LYAH using emacs.
My current Set up:
- Ubuntu 12.04 LTS (Use Experience – beginner)
- GNU Emacs 23 (Use Experience – beginner)
- able to work in haskell major mode
Finding difficult to follow instructions (to bring haskell libraries) at Point 2 described here.
Also need guidance to enable Scion IDE.
Problem:
.hs code
data Maybe a = Nothing | Just a
While running the code, I get following error:
Please ignore typo error, Originally posted:
*Main> just "Haha"
interactive>:339:1: Not in scope: `just’
This is the real error (added after Tikhon Jelvis’ comment):
*Main> Just "Haha"
interactive>:341:1:
Ambiguous occurrence `Just'
It could refer to either `Main.Just',
defined at /home/optimight/baby.hs:89:26
or `Prelude.Just',
imported from `Prelude' at /home/optimight/baby.hs:1:1
(and originally defined in `Data.Maybe')
Your error just tells you that there are two possible versions of
Justand GHCi does not know which one to pick.Every Haskell program implicitly imports a whole bunch of functions and data types. These form the “prelude”. One of these types is
Maybe. This means that every single program already has access to a type just like the one you defined, with exactly the same names.You can overcome this in two ways. The best option would be to just choose different names:
(I’m sure you can come up with better names than that though :P.)
The other option would be to import the Prelude explicitly, hiding
Maybe:This line at the top of your program just tells Haskell to import everything it normally imports except
Maybeand all of it’s constructors (JustandNothing).Now you will run into one more problem: GHCi does not know how to render a value of your
Maybetype into a string to show on the prompt. You will get an error like this:What you need to do is tell the compiler how a
Maybevalue looks as a string. Happily, this is extremely easy. In fact, it’s so easy even the computer can do it! If you define your type as:then the compiler will write a
showfunction (which is basicallytoStringfrom other languages) for you. Now your original statement (Just "Haha") should work properly.Also: enabling Scion is a completely different question entirely. I don’t think it’s worth bothering with it until you’ve learned more Haskell and are actually working on some sort of larger project. For now, the standard Haskell mode should be more than enough.