Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,257 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,189 people online right now. Registration is fast and FREE... Join Now!




Using Lambda to Create a Function

 
Reply to this topicStart new topic

Using Lambda to Create a Function

LowWaterMark
11 Oct, 2008 - 02:45 AM
Post #1

D.I.C Head
**

Joined: 30 Jul, 2008
Posts: 119



Thanked: 1 times
My Contributions
I'm struggling to understand how and when to use of lambda to create a function. When does it becomes necessary or helpful? Consider the following two inexact (Scheme) codes for expressing the area of a disk:

CODE
(define (area-of-disk r)
   (* 3.14 (* r r)))


CODE
(define area-of-disk
   (lambda (r)
      (cond
         ((* 3.14 (* r r)))))


Please explain how and when the utility of the second snippet would supersede that of the first.

Thanks for reading.

This post has been edited by LowWaterMark: 11 Oct, 2008 - 02:47 AM
User is offlineProfile CardPM
+Quote Post

LowWaterMark
RE: Using Lambda To Create A Function
11 Oct, 2008 - 09:49 PM
Post #2

D.I.C Head
**

Joined: 30 Jul, 2008
Posts: 119



Thanked: 1 times
My Contributions
I guess I'm looking for an Archimedes "a ha" moment here where lambda-calculus is concerned. If anyone read something that turned on the light-bulb over their head, please send me a link.
User is offlineProfile CardPM
+Quote Post

LowWaterMark
RE: Using Lambda To Create A Function
13 Oct, 2008 - 12:51 AM
Post #3

D.I.C Head
**

Joined: 30 Jul, 2008
Posts: 119



Thanked: 1 times
My Contributions
Allow me to paraphrase, bastardize and plagiarize. "Recursive functionality, lambda calculus and Turing computability are mathematically equivalent." See Church/Turing and Wang.

Or,

"The lambda calculus or λ-calculus is another way of defining computability, equivalent to Turing machines and partial recursive functions."

Assume just that lambda calculus and recursive functionality are mathematically equivalent. That said, you can use lambda calculus to define any recursive procedure. Now, I understand recursive functionality. Given that predicate understanding, can anyone shed some light on lambda calculus for me?

Why am I so stuck? Dammit! Bukowski (see below) was right and you guys are ignoring me sad.gif . Take pity on my conceptually challenged soul.

This post has been edited by LowWaterMark: 13 Oct, 2008 - 01:03 AM
User is offlineProfile CardPM
+Quote Post

Moonbat
RE: Using Lambda To Create A Function
13 Oct, 2008 - 01:53 AM
Post #4

D.I.C Regular
Group Icon

Joined: 30 Jun, 2008
Posts: 391



Thanked: 22 times
Dream Kudos: 600
My Contributions
Hey, lambda functions gave me a migraine too biggrin.gif

But after reading this RFC for lambda functions in PHP, I only get slight headaches smile.gif
User is offlineProfile CardPM
+Quote Post

LowWaterMark
RE: Using Lambda To Create A Function
13 Oct, 2008 - 02:18 AM
Post #5

D.I.C Head
**

Joined: 30 Jul, 2008
Posts: 119



Thanked: 1 times
My Contributions
Thank you so much. I'm already reading the link. - appreciate it!

Take care.
User is offlineProfile CardPM
+Quote Post

LowWaterMark
RE: Using Lambda To Create A Function
14 Oct, 2008 - 12:52 AM
Post #6

D.I.C Head
**

Joined: 30 Jul, 2008
Posts: 119



Thanked: 1 times
My Contributions
Author after author keeps referring to the tenor of elegance and simplicity of functional coding (e.g. lambda, Scheme, Lisp). I just read this intro which, using metaphor, shed some light, but I still don't get it. Does the following excerpt jive with anyone?

QUOTE
Functions and Lambda Notation
A function accepts input and produces an output. Suppose we have a "chocolate-covering" function that produces the following outputs for the corresponding inputs:
peanuts -> chocolate-covered peanuts
rasins -> chocolate-covered rasins
ants -> chocolate-covered ants

We can use Lambda-calculus to describe such a function:
Lx.chocolate-covered x

This is called a lambda-expression. (Here the "L" is supposed to be a lowercase Greek "lambda" character).
If we want to apply the function to an argument, we use the following syntax:

(Lx.chocolate-covered x)peanuts -> chocolate-covered peanuts

Functions can also be the result of applying a lambda-expression, as with this "covering function maker":

Ly.Lx.y-covered x

We can use this to create a caramel-covering function:
(Ly.Lx.y-covered x)caramel -> Lx.caramel-covered x
(Lx.caramel-covered x)peanuts -> caramel-covered peanuts

Functions can also be the inputs to other functions, as with this "apply-to-ants" function:

Lf.(f)ants

We can feed the chocolate-covering function to the "apply-to-ants" function:
(Lf.(f)ants)Lx.chocolate-covered x
-> (Lx.chocolate-covered x)ants
-> chocolate-covered ants



The key is in that quote somewhere, I know it. I got lost when the author merged lambda syntax with algebraic syntax in the "apply to ants" function: Lf.(f)ants. I was doing great until the end there.

I'm on day-4 and going nuts. I hate not understanding something, especially given that I do understand recursion, its mathematical equivalent. It seems that I should be able to intuit Lambda but that insight just ain't coming along.

This post has been edited by LowWaterMark: 14 Oct, 2008 - 01:28 AM
User is offlineProfile CardPM
+Quote Post

rahulbatra
RE: Using Lambda To Create A Function
14 Oct, 2008 - 01:47 AM
Post #7

D.I.C Head
Group Icon

Joined: 28 Dec, 2005
Posts: 161


Dream Kudos: 275
My Contributions
See if this helps,

Defining a function f(x) = (x + 1)

Putting this into prefix syntax with Scheme keyword define,

CODE

(define (f x) (+ x 1))


You can also use the lambda to do the same, the only difference being the syntax

CODE

(define f (lambda (x) (+ x 1)))



What you're doing here is that you're actually associating the lambda with the operation of "x+ 1". You then associate the function f with this lambda.

I'm not sure why you put a "cond" expression in your syntax, but think of a lambda as a no-name collection of operations which can then be binded to a named function taking specific parameters.
User is offlineProfile CardPM
+Quote Post

LowWaterMark
RE: Using Lambda To Create A Function
14 Oct, 2008 - 02:03 AM
Post #8

D.I.C Head
**

Joined: 30 Jul, 2008
Posts: 119



Thanked: 1 times
My Contributions
QUOTE
What you're doing here is that you're actually associating the lambda with the operation of "x+ 1". You then associate the function f with this lambda.


So, is the deal that now I have a new primitive, "f" which can employ any argument, "x" in its operation while simultaneously retaining the capacity to be applied within another unrelated lambda expression, since functions are perfectly good arguments (ala Alonzo Church, circa 1938).

Where are my manners? Thanks for taking the time to reply. It did help.

This post has been edited by LowWaterMark: 14 Oct, 2008 - 02:08 AM
User is offlineProfile CardPM
+Quote Post

rahulbatra
RE: Using Lambda To Create A Function
14 Oct, 2008 - 02:28 AM
Post #9

D.I.C Head
Group Icon

Joined: 28 Dec, 2005
Posts: 161


Dream Kudos: 275
My Contributions
Yes, functions can be passed around as arguments, which forms the basis of higher order functions in a functional programming language.

To do the proper bottom-up approach in FP, you create basic functions which are passed around as args to other more complicated ones.

For example, I guess you're reading HTDP - if I recall correctly there is an example where the area-of-circle is passed around as args to area-of-disk or something like that.

Of course, you'll have to refer to a more thorough reference in Lambda calculus - I am in all honesty, just a newbie. If you do find something wrong in my approach to lambda later, I'll be happy to correct myself.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 04:32AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month