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]
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
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With