[Haskell-beginners] Re: Closure

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Sat Aug 15 12:34:25 EDT 2009


On Aug 15, 2009, at 12:07 , Daniel Bastos wrote:
> This makes sense. Because, even in a language like C, a similar effect
> can be achieved, no? For example
>
> int plus(int x, int y) { return x + y; }
>
> int plus3(int y) { plus(3, y); }
>
> So, what I can't do in C, besides almost everything I can't do, is to
> do this nicely like I do in Haskell. But we don't call this a
> closure. In fact, we say C does not allow for closures. So what am I
> missing?


In C you have to declare it.  In Haskell you can just do it on the fly:

 > map (add 3) [1..10]

or indeed

 > map (+3) [1..10]

(The above is actually a "section", a closure created from an infix  
function.  The difference is that I can specify either side of the  
operator, whereas to specify arguments other than the first for a  
regular function I must either coerce it into infix with `` or use the  
"flip" function to rearrange arguments.)

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/beginners/attachments/20090815/c71a43c5/PGP.bin


More information about the Beginners mailing list