Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Haskell: Sections


Sections are a convenient syntax for partial application of binary Operator. Sections are written as (operator e) or ( e operator), where operator is a binary operator and e is an expression. For a section, we only give one of the arguments to the infix operator, and it represents a function, which intuitively takes an argument and puts it on the "missing" side of the infix operator.
*Main> map (2+) [2, 3, 5, 7]
[4,5,7,9]
*Main>
*Main> map (*3) [2, 3, 5, 7]
[6,9,15,21]
*Main>
*Main> map (/3) [2, 3, 5, 7]
[0.6666666666666666,1.0,1.6666666666666667,2.3333333333333335]

In above snippet (+2), (*3), (/3) are functions.

While using sections, make sure you are omitting parameters at correct place.
For example,
 map (^2) [1, 2, 3, 4] calculates 1^2, 2^2, 3^2, 4^2
 map (2^) [1, 2, 3, 4] calculates 2^1, 2^2, 2^3, 2^4


*Main> map (^2) [1, 2, 3, 4]
[1,4,9,16]
*Main>
*Main> map (2^) [1, 2, 3, 4]
[2,4,8,16]



Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Haskell: Sections

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×