Nayana
Nayana

Reputation: 1549

Haskell indentation error. Elementary level

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?

Upvotes: 0

Views: 390

Answers (2)

ertes
ertes

Reputation: 4450

You have just confused apostrophes with backticks in line 11:

fst t 'elem' primes

should be:

fst t `elem` primes

Upvotes: 7

yatima2975
yatima2975

Reputation: 6610

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 let in let 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 partC and partD, you write t <- [zip num fibs/primes]. This means that t is bound to each element of the one-element list [zip num ...] in turn. zip num fibs/primes is 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 in partC and partD) is prime or a Fibonacci number by checking whether it's an element of primes or fibs, that will work when the number is actually in the list, but it won't return False if it's not. This is because elem doesn't 'know' that the lists you're looking through are sorted in ascending order. For example, if you try to evaluate elem 4 primes it'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 smarter elem-like function.

Upvotes: 8

Related Questions