I’m still trying to crack this code:
import Data.Char
groupsOf _ [] = []
groupsOf n xs =
take n xs : groupsOf n ( tail xs )
problem_8 x = maximum . map product . groupsOf 5 $ x
main = do t <- readFile "p8.log"
let digits = map digitToInt $concat $ lines t
print $ problem_8 digits
In problem_8 x = maximum . map product . groupsOf 5 $ x
why can’t it just be groupsOf 5 x ?
is it because x will later be expanded to some other expressions(here it will be: digits = map digitToInt $concat $ lines t ) ? is this the so-called lazy(x wont be expanded now, but maybe later) ?
Without the
$, the precedence works out like this:Since
.(function composition) takes two functions as arguments, andgroupsOf 5 xcannot return a function, this is an error.With the
$, the precedence works out like this:This is equivalent (via function composition) to:
or:
(however stringing along
$like this is considered poor style)This has nothing to do with laziness, note.