Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Haskell, why do i have to use the dollar sign in this code?

Tags:

haskell

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) ?

like image 565
McBear Holden Avatar asked Nov 25 '11 23:11

McBear Holden


People also ask

Is Dollar sign an operator?

The dollar sign, $ , is a controversial little Haskell operator. Semantically, it doesn't mean much, and its type signature doesn't give you a hint of why it should be used as often as it is. It is best understood not via its type but via its precedence.

What does () mean in Haskell?

() is very often used as the result of something that has no interesting result. For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO () .

What is the operator in Haskell?

Haskell provides special syntax to support infix notation. An operator is a function that can be applied using infix syntax (Section 3.4), or partially applied using a section (Section 3.5).

What is flip Haskell?

flip f takes its (first) two arguments in the reverse order of f.


3 Answers

Without the $, the precedence works out like this:

maximum . map product . (groupsOf 5 x)

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

With the $, the precedence works out like this:

(maximum . map product . groupsOf 5) x

This is equivalent (via function composition) to:

maximum (map product (groupsOf 5 x))

or:

maximum $ map product $ groupsOf 5 x

(however stringing along $ like this is considered poor style)

This has nothing to do with laziness, note.

like image 127
bdonlan Avatar answered Nov 03 '22 09:11

bdonlan


You don't have to use $, in fact, you never have to use it.

In your case, the pointless^H^H^H^Hsfree notation suggests itself: Instead of

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 out the x on both sides of the equation:

problem_8 = maximum . map product . groupsOf 5
like image 42
Ingo Avatar answered Nov 03 '22 09:11

Ingo


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

Use hoogle to figuring out what some keyword are doing. It also could be helpful to showing function signature and module name where it's from.

like image 32
ДМИТРИЙ МАЛИКОВ Avatar answered Nov 03 '22 10:11

ДМИТРИЙ МАЛИКОВ