1

I'm writing a short program in haskell to find the mean of the list of odd numbers between 1 and 2000 that are not divisible by 3 or 5. I can't get it to compile and keep getting a variety of errors. I made some changes and now the code is giving me a "Parse error on input 'sum'" on line 5 col 9. Could someone tell me what I'm doing wrong please?

--Write a Haskell function that calculates the mean of a list of odd numbers that
--are not divisible by 3 or 5 and whose sum is less than 2000.
mean :: Int
mean = let nums = [x|x <- [1,3..1999], x 'mod' 3 != 0, x 'mod' 5 != 0]
    sum nums/length nums

I'm compiling with GHCI. Thanks

2
  • Is this homework, and is the comment in the code the original task description? If so, I'd say you have misunderstood it. Commented Aug 7, 2013 at 19:39
  • Yes the comment at the top is the original question. It's from a past exam paper that I was working through to study for an exam. Why do you think it's wrong? Commented Aug 8, 2013 at 10:32

2 Answers 2

4

Besides the missing in as mentioned by DiegoNolan there are some other minor problems with your definition. First, to use a two-ary function infix, you have to enclose it in backticks ` while you use ticks '. Furthermore, Haskell uses /= for inequality and not !=. Finally, you cannot use / for integer division but function div.

mean = let nums = [x|x <- [1,3..1999], x `mod` 3 /= 0, x `mod` 5 /= 0]
       in
       sum nums `div` length nums
Sign up to request clarification or add additional context in comments.

3 Comments

you have a typo, numbs should be nums
Actually, I'd consider it an error if a function to compute the mean silently truncated the result.
Thanks for your help, I realised I had those errors too after I added the "in".
2

you need in

mean :: Int
mean = let nums = [x|x <- [1,3..1999], x 'mod' 3 != 0, x 'mod' 5 != 0]
       in sum nums/length nums

2 Comments

Thanks, that helped, I had to fix a few other things too. I was using the wrong quotes for mod and the not equals operator is /= instead != and I had to change the / symbol on the last line to div. It works fine now
@user1786327 You might want to use fromIntegral to convert to a rational or floating point type. A mean of integers isn't really an integer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.