I am taking a class that introduces Haskell.
I am completely new to this language.
when I compile it, it says:
[1 of 1] Compiling Main ( hw1.hs, interpreted )
hw1.hs:11:1: parse error (possibly incorrect indentation)
Failed, modules loaded: none.
Could anybody explain what I’ve done wrong?
Thank you very much.
p.s.
I got a snippet of the code (called primes) from online.
I do not understand that code, if anyone could elaborate on it, that’d be nice.
I don’t understand what the function, sieve, does. Is it a built-in haskell function?
In addition to what @ertes said about the backticks, there’s some more things wrong. Two of them are easily fixable, fortunately!
First of all, the
letinlet num = [1..]should be deleted: in an .hs file you don’t need these (but you do in GHCi, that might be confusing at first). This is what gave you the indentation error. Once you fix that, you’ll get some type errors.Secondly, in
partCandpartD, you writet <- [zip num fibs/primes]. This means thattis bound to each element of the one-element list[zip num ...]in turn.zip num fibs/primesis already a list so you don’t need the brackets. After fixing this, the program compiles, but doesn’t work properly.Lastly, if you try to determine whether a number (in your case
fst t, again inpartCandpartD) is prime or a Fibonacci number by checking whether it’s an element ofprimesorfibs, that will work when the number is actually in the list, but it won’t returnFalseif it’s not. This is becauseelemdoesn’t ‘know’ that the lists you’re looking through are sorted in ascending order. For example, if you try to evaluateelem 4 primesit’s going to check whether 4 is equal to 2,3,5,7,11, and so on – who knows, a 4 might eventually turn up! To fix this you’ll have to write a slightly smarterelem-like function.