largestDivisible :: (Integral a) => a
largestDivisible = head (filter p [100000,99999..])
where p x = x `mod` 3829 == 0
If p x equals True,
does
head (filter p [100000,99999..])
become
head (filter True)
?
What list is being filtered for True?
While this code is being run, what are p and x’s values?
filter p [100000,99999..]calculates the list including all numbers descending from 100000 for which p returns true.headthen takes the first of that list, effectively giving you the largest number x below 100000, for whichp xreturns true, i.e. for whichx `mod` 3829is 0.p is a function that takes one argument called
xand returns true iffx `mod` 3829 == 0.xis 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).