accumulator and lambda

3 views
Skip to first unread message

maya

unread,
Nov 6, 2007, 8:55:39 AM11/6/07
to Principles of Programming with JavaScript
What are the advantages of accumulator and lambda?

Venkatesh Choppella

unread,
Nov 6, 2007, 1:09:26 PM11/6/07
to popjs-20...@googlegroups.com
Okay, there are two questions here, I'll do accumulator in a separate email.

1. Lambda is the function constructor. Since functions are
"first-class" values, like numbers, and lists, and
strings, we need a way to construct them. Notice that
the function created by lambda is anonymous; it does not
have a name. The other way to create functions is with
(define (f x) ...) notation. The define notation creates
a function and also binds it to the variable, in this
case f. Anonymous functions are very useful to have
because in Scheme we often create functions "on-the-fly",
i.e., dynamically. Consider for example:

(define (addc x)
(lambda (y)
(+ x y)))

The value returned by (addc 2), for example is a function
that adds 2 to its argument. We could have tried to write
it like the following, forcing the inner function to be named to,
say, g.

(define (add2 x)
(define (g y)
(+ x y))
g)

But the lambda is considered more compact, and also
general. Because
(define (f x) body) is the same as

(define f (lambda (x) body))

The second form separates the creation of a value (due to
the lambda constructor) and its binding with an identifier,
which is nice due its conceptual clarity compared to the
first form.

Best Regards.
- venkatesh


On 11/6/07, maya <maya...@iiitmk.ac.in> wrote:
>
> What are the advantages of accumulator and lambda?
>
>
> >
>


--
Venkatesh Choppella, PhD.
Associate Professor
Indian Inst. of Info. Tech. & Mgmt -- Kerala
Park Centre, Technopark, Kariavottam P.O.
Trivandrum, Kerala 695 581, INDIA
home: http://www.iiitmk.ac.in/~choppell
calendar: http://www.google.com/calendar/embed?src=chop...@gmail.com

Reply all
Reply to author
Forward
0 new messages