Reputation: 43
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
Upvotes: 1
Views: 664
Reputation: 3173
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
Upvotes: 4
Reputation: 3766
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
Upvotes: 2