Search code examples
haskellghci

load and use code file in haskell


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 .


Solution

  • 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]