P. 1
Haskell Programming Language

Haskell Programming Language

|Views: 157|Likes:
Published by Saira Ali

More info:

Published by: Saira Ali on May 17, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
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 infix 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 infix operators (like +), we can
cause the function to become prefix by enclosing it in paretheses. For example:

73

74

CHAPTER 7. ADVANCED FEATURES

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-infix functions can be made infix by enclosing them in backquotes (“`’’). For

example:

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

You're Reading a Free Preview

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