Reputation: 13
I am brand new to haskell and functional programming in general.
How can I create a function that finds all odd numbers less than 200 that are divisible by 3 and 7 using only list comprehension?
this is my code:
oddsDivisible3and7 :: Integer -> Integer -> Integer -> Integer -> [Integer]
oddsDivisible3and7 xs = [x | x <- [1..xs],x mod 3 == 0 && x mod 7 == 0,x < 200]
and the errors it is throwing:
• Couldn't match expected type ‘(Integer -> Integer -> Integer)
-> Integer -> Integer’
with actual type ‘Integer’
• The function ‘x’ is applied to two arguments,
but its type ‘Integer’ has none
In the first argument of ‘(==)’, namely ‘x mod 3’
In the first argument of ‘(&&)’, namely ‘x mod 3 == 0’
with another block for the mod 7
I'm not looking for a written function, I just need some guidance.
Upvotes: 1
Views: 395
Reputation: 16224
There are a few type error and a few sintax error, let me show:
oddsDivisible3and7 :: Integral a => a -> [a]
oddsDivisible3and7 n = [x | x <- [1..n],
x `mod` 3 == 0 && x `mod` 7 == 0 && x < 200 && x `mod` 2 /= 0]
first of all the type should be: Integral a => a -> [a]
Then, you want the divisible by 3, 7 and the odds (not divisible by 2), and all less than 200.
example:
oddsDivisible3and7 500
=> [21,63,105,147,189]
Upvotes: 2