Take the 2-minute tour ×
largestDivisible :: (Integral a) => a
largestDivisible = head (filter p [100000,99999..])
    where p x = x `mod` 3829 == 0

If p x equals True,


head (filter p [100000,99999..])


head (filter True)


What list is being filtered for True?

While this code is being run, what are p and x's values?

share|improve this question
Your question is incomprehensible—I'm not sure what you're actually asking. –  Antal S-Z Jun 2 '10 at 18:59
add comment

2 Answers

up vote 4 down vote accepted

filter p [100000,99999..] calculates the list including all numbers descending from 100000 for which p returns true. head then takes the first of that list, effectively giving you the largest number x below 100000, for which p x returns true, i.e. for which x `mod` 3829 is 0.

What values are in p and x?

p is a function that takes one argument called x and returns true iff x `mod` 3829 == 0. x is the argument given to the function. Since you use p as an argument to filter, this means that each element of the list [100000,99999..] will be given to p in turn, until p returns true for the first time (it won't try any more elements because by using head, you're only requesting one element, so it only calculates one).

share|improve this answer
When does p return true? –  Delirium tremens Jun 2 '10 at 19:14
@Deliriumtremens: p x returns true if (and only if) x `mod` 3829 == 0, i.e. if x is divisible by 3829 without remainder. –  sepp2k Jun 2 '10 at 19:28
add comment

p is a function defined by p x = x `mod` 3829 == 0.

x is a variable in the p function. filter calls p with elements from the list [100000,99999..], so x will be one of the members of that list.

filter p [100000,99999..] is the same as (filter p) [100000,99999..], not filter (p [100000,99999..]). So p is not called with [100000,99999..] as an argument (and it would be a type error anyway).

share|improve this answer
add comment

Your Answer


