0
votes

I'm trying to zip two lists together but I cannot for the life of me figure out why it won't run the code. I have two lists declared as variables, and I've written a function to combine them together.

letters = ["a","b","c"]
numbers = ["1","2","3"]

comb :: [a] -> [b] -> [(a,b)]
comb _ [] = []
comb [] _ = []
comb (x:xs)(y:ys) = (x,y):comb xs ys

My hope here is that this code will recursively combine the pairs into tuples, but I'm very new to Haskell so I'm not even sure if what I've written is functional (the compiler doesn't complain about it).

My issue comes up when I try to run the code with this line,

comb letters numbers

and the compiler tells me "Parse error: module header, import declaration or top-level declaration expected." I'm not sure what I'm supposed to declare here. Can I please have some assistance?

1
Did you just put that comb letters numbers expression in the middle of your file?melpomene
It is at the very bottom of my file. I declare the lists, write the code, and then have the comb letters numbers lineremington howell

1 Answers

4
votes

You can't just put expressions in a file. In other words, you don't need comb; your problem could be reduced to

2 + 2

What you probably should do is load the file in the interactive interpreter of your choice (ghci) and then type expressions there, which will work.

Or you could provide a definition for main in your file:

main = print (comb letters numbers)

This should allow it to be compiled into an executable successfully.

But the point is, a file is a series of declarations. The entry point for your program is a symbol called main. Whatever you define main as is what gets run when you start your program (and that thing must be an IO action, such as the one returned by print).