Why do both maps (^ 2) xs and map (2 ^) xs work as expected in Haskell?

Why

map (^2) [1..10]

and

map (2^) [1..10]

Job?

I expect it to work with only one of them, and not for both.

I thought the map would iterate over all the elements [1..10]and then do

[1^2, 2^2, 3^2, ...]

for map (^2) [1..10]. Then I would expect that when given, map (2^) [1..10]this would lead to a sintax error or something else, because that would require a number after ^, not before.

+5
source share
3 answers

Haskell , " ". - , say #$%, :

(#$%)   = \x y -> x #$% y
(#$% y) = \x   -> x #$% y
(x #$%) = \y   -> x #$% y

, , , , Forth, . , , " , ".

( - f + x f , + ? , ? : (), .)

+16

Haskell , ^ , ( ) (2^) f(x) = 2^x (^2) f(x) = x^2.

+3

(2 ^) creates a new function that takes a parameter and calls (parameter 2 ^). This is a curryfication way.

Here's a link to the theory behind it: Partial Application (Haskell.org)

0
source

All Articles