In Haskell, why should I use the dollar sign in this code?

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 problem_8 x = maximum . map product . groupsOf 5 $ x problem_8 x = maximum . map product . groupsOf 5 $ x why can't it just be groups? is it because x will later be expanded to some other expressions (here it will be: digits = map digitToInt $concat $ lines t )? is it the so-called lazy (x will not expand now, but maybe later)?

+7
source share
3 answers

Without $ priority works as follows:

 maximum . map product . (groupsOf 5 x) 

So how . (function composition) takes two functions as arguments, and groupsOf 5 x cannot return the function, this is an error.

With $ , priority works as follows:

 (maximum . map product . groupsOf 5) x 

This is equivalent (via composition of functions):

 maximum (map product (groupsOf 5 x)) 

or

 maximum $ map product $ groupsOf 5 x 

(however, a line along $ , as it is considered bad style)

This has nothing to do with laziness, please note.

+15
source

You do not need to use $ , in fact you will never have to use it.

In your case, the meaningless designation is HHHHHHHHFHHHFHHHHFHHH HHFHHHHHHFHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

 problem_8 x = maximum . map product . groupsOf 5 $ x 

which is equivalent to:

 problem_8 x = (maximum . map product . groupsOf 5) x 

we can leave x on both sides of the equation:

 problem_8 = maximum . map product . groupsOf 5 
+8
source

As bdonlan said, $ is an application function and . is a functional composition .

Use hoogle to find out what a particular keyword is doing. It may also be useful to show the signature of the function and the name of the module in which it is located.

+5
source

All Articles