Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the grave accent is used when passing the mod function to map?

Tags:

haskell

I'm learning Haskell, but I didn't find an answer to this.

Why the grave accent is used to pass the mod function to map like in the example? I saw other cases with other functions where it isn't needed.

map (`mod` 3) [1..6]   -- result is [1,2,0,1,2,0]

If I pass without the grave accent, the result is completely different.

map (mod 3) [1..6]    -- result is [0,1,0,3,3,3]
like image 458
Renato Dinhani Avatar asked Jan 28 '13 18:01

Renato Dinhani


2 Answers

The accent "makes the function behave like an operator". Eg:

mod a b == a `mod` b

so

(mod 3) == mod 3 ?

and

(`mod` 3) == mod ? 3
like image 105
Alex Oliveira Avatar answered Nov 15 '22 08:11

Alex Oliveira


If you want to explicitly sure about what are you thinking about, (I always do mine since I am still in learning phase too), you can always use anonymous function (I think sometimes called lambda expression, but not sure)

> map (\x -> x `mod` 3) [1..10]
[1,2,0,1,2,0,1,2,0,1]

> map (\x -> 3 `mod` x) [1..10]
[0,1,0,3,3,3,3,3,3,3]

> map (\x -> mod x 3) [1..10]
[1,2,0,1,2,0,1,2,0,1]

> map (\x -> mod 3 x) [1..10]
[0,1,0,3,3,3,3,3,3,3]
like image 28
wizzup Avatar answered Nov 15 '22 06:11

wizzup