Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can (++) be used both to prepend and append using map?

I'm currently getting started with Haskell (reading Learn Yourself a Haskell), and came across lines akin to the following:

map (++"!") ["a", "b"] -- ["a!", "b!"]
map ("!"++) ["a", "b"] -- ["!a", "!b"]

Why is this possible, or how does it work? I can't manage to do the same with other non-commutative operations, like division:

map (3/) [1..3]   -- [3.0,1.5,1.0]
map ((/)3) [1..3] -- [3.0,1.5,1.0]
map (3(/)) [1..3] -- error

I feel like I'm missing something here, but the implementation of map doesn't give me any hints.

like image 535
Kolja Avatar asked Dec 09 '22 12:12

Kolja


1 Answers

This code is not valid:

map (3(/)) [1..3]

(/) is prefix function but you use it as infix. Compiler see it as you try to function 3 (a function without arguments), add (/) as an argument.

/ is infix function. So, you can do next:

map ( / 3) [1..3]   -- [0.3333333333333333,0.6666666666666666,1.0]
map (3 / ) [1..3]   -- [3.0,1.5,1.0]
like image 157
viorior Avatar answered Dec 14 '22 20:12

viorior