P. 1

|Views: 157|Likes:

### Availability:

See more
See less

04/03/2013

pdf

text

original

We’ve already seen how to double the values of elements in a list using map:

Prelude> map (\x -> x*2) [1,2,3,4]
[2,4,6,8]

However, there is a more concise way to write this:

Prelude> map (*2) [1,2,3,4]
[2,4,6,8]

This type of thing can be done for any inﬁx function:

Prelude> map (+5) [1,2,3,4]
[6,7,8,9]
Prelude> map (/2) [1,2,3,4]
[0.5,1.0,1.5,2.0]
Prelude> map (2/) [1,2,3,4]
[2.0,1.0,0.666667,0.5]

You might be tempted to try to subtract values from elements in a list by mapping
-2 across a list. This won’t work, though, because while the + in +2 is parsed as
the standard plus operator (as there is no ambiguity), the - in -2 is interpreted as the
unary minus, not the binary minus. Thus -2 here is the number −2, not the function
λx.x−2.

In general, these are called sections. For binary inﬁx operators (like +), we can
cause the function to become preﬁx by enclosing it in paretheses. For example:

73

74

Prelude> (+) 5 3
8
Prelude> (-) 5 3
2

Additionally, we can provide either of its argument to make a section. For example:

Prelude> (+5) 3
8
Prelude> (/3) 6
2.0
Prelude> (3/) 6
0.5

Non-inﬁx functions can be made inﬁx by enclosing them in backquotes (“`’’). For

example:

Prelude> (+2) ‘map‘ [1..10]
[3,4,5,6,7,8,9,10,11,12]

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->