0

I'm just starting to learn haskell and i dont really understand how to use files i created with an ordinary editor in the GHCi interpreter...

This is my file list-comprehension.hs

main = do
let substantive = [" Student", " Professor", " Tutor"]
let adjektive = ["fauler", "fleissiger", "hilfreicher"]
let tupel = [a ++ s | a <- adjektive, s <- substantive]
return (tupel)

When I load the file in GHCi it works alright, but then I cant actually use it. So when I try to execute tupel, it wont work.

Prelude> :load list-comprehension.hs
[1 of 1] Compiling Main             ( list-comprehension.hs, interpreted )
Ok, modules loaded: Main.

*Main> tupel   
<interactive>:3:1: error: Variable not in scope: tupel

This also happens when I try to get the other variables. I have researched this a lot, but I cant find out whats wrong with my file or how this generally works... I'm not at all sure about the "main = do" and the "return" part, but this is the only beginning and end that doesnt produce a parse error when loading .

1
  • if you want to use the definitions in GHCI, define them outside of the main block, without the let statement. Commented May 1, 2017 at 16:33

1 Answer 1

1

GHCi only has the top level definitons from a file in scope. Try this:

main :: IO ()
main = print tupel

substantive :: [String]
substantive = [" Student", " Professor", " Tutor"]

adjektive :: [String]
adjektive = ["fauler", "fleissiger", "hilfreicher"]

tupel :: [String]
tupel = [a ++ s | a <- adjektive, s <- substantive]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! I think I understand what you did, ill read up on this ;)
Just to be clear, the type signatures aren't mandatory, but you should definitely use them to help the people who read your code, including yourself.

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.