Reputation: 1687
I want to list all integers that divide n. This is a homework question. So far I have done this.
divisors :: Int -> [Int]
divisors n | n < 1 = []
| otherwise = filter (\n -> n `mod` x == 0) [1..n]
where x = [1..n]
I know this is wrong, but I am not getting the right filter predicate. I don't know how the syntax is for doing this. and ofcourse I cannot use n mod
n since that is just lists all elements 1 to n.
Upvotes: 0
Views: 202
Reputation: 183868
You want to check if mod n k == 0
for each k
from 1 to n
. The n
is fixed (the argument of divisors
) and the k
varies, i.e. that is what should be the argument of the lambda expression
| otherwise = filter (\k -> n `mod` k == 0) [1 .. n]
Upvotes: 4
Reputation: 59811
I don't know what you are trying to do, but the type of mod
is
mod :: Integral a => a -> a -> a
You call it with an Integral argument and a list of integral arguments.
Upvotes: 0