Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using let in .hs file

Tags:

haskell

I'm using Notepad++ and WinGHCi to do some homework and I have to define a little "database". The format is arbitrary and I don't think that's where I'm going wrong. Anyway, here's what I'm using in a *.hs file:

let studentDB = [
                ("sally",   ["cpsc110", "cpsc312", "cpsc204"]),
                ("jim",     ["cpsc110", "cpsc313"]),
                ("bob",     ["cpsc121", "cpsc303", "cpsc212"]),
                ("frank",   ["cpsc110", "cpsc212", "cpsc204"]),
                ("billy",   ["cpsc312", "cpsc236"]),
                ("jane",    ["cpsc121"]),
                ("larry",   ["cpsc411", "cpsc236"]) ]

WinGHCi gives me this error: a1.hs:118:1: parse error (possibly incorrect indentation)

I tried messing tabbing the tuples over or and placing my list brackets on different lines but couldn't get anything to work. I thought something smaller would help me track the bug so I did this instead:

let s = []

But that gave me the same error. Is this an indentation error, maybe due to some quirky Notepad++ behavior? Or is my Haskell wrong? Thanks.

like image 336
user966249 Avatar asked Sep 27 '11 04:09

user966249


Video Answer


1 Answers

I imagine you're thinking that the contents of a *.hs file are like what you can type into ghci. That's incorrect. When you're typing into ghci you're effectively typing into a do block. So the following syntax is correct:

main = do
    let s = []
    -- do more stuff

However, at the top level of a *.hs file, things are different. The let construct is actually

let s = [] in
    codeThatReferencesS

If you want to define a top-level binding, just say

s = []
like image 182
Lily Ballard Avatar answered Oct 15 '22 19:10

Lily Ballard