8 Replies - 2097 Views - Last Post: 14 October 2008 - 03:28 AM

#1 LowWaterMark   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 30-July 08

Using Lambda to Create a Function

Post icon  Posted 11 October 2008 - 03:45 AM

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:

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


(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 October 2008 - 03:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using Lambda to Create a Function

#2 LowWaterMark   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 30-July 08

Re: Using Lambda to Create a Function

Posted 11 October 2008 - 10:49 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 LowWaterMark   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 30-July 08

Re: Using Lambda to Create a Function

Posted 13 October 2008 - 01:51 AM

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 :( . Take pity on my conceptually challenged soul.

This post has been edited by LowWaterMark: 13 October 2008 - 02:03 AM

Was This Post Helpful? 0
  • +
  • -

#4 Moonbat   User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: Using Lambda to Create a Function

Posted 13 October 2008 - 02:53 AM

Hey, lambda functions gave me a migraine too :D

But after reading this RFC for lambda functions in PHP, I only get slight headaches :)
Was This Post Helpful? 0
  • +
  • -

#5 LowWaterMark   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 30-July 08

Re: Using Lambda to Create a Function

Posted 13 October 2008 - 03:18 AM

Thank you so much. I'm already reading the link. - appreciate it!

Take care.
Was This Post Helpful? 0
  • +
  • -

#6 LowWaterMark   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 30-July 08

Re: Using Lambda to Create a Function

Posted 14 October 2008 - 01:52 AM

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 October 2008 - 02:28 AM

Was This Post Helpful? 0
  • +
  • -

#7 rahulbatra   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 187
  • Joined: 28-December 05

Re: Using Lambda to Create a Function

Posted 14 October 2008 - 02:47 AM

See if this helps,

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

Putting this into prefix syntax with Scheme keyword define,

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



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

(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.
Was This Post Helpful? 0
  • +
  • -

#8 LowWaterMark   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 30-July 08

Re: Using Lambda to Create a Function

Posted 14 October 2008 - 03:03 AM

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 October 2008 - 03:08 AM

Was This Post Helpful? 0
  • +
  • -

#9 rahulbatra   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 187
  • Joined: 28-December 05

Re: Using Lambda to Create a Function

Posted 14 October 2008 - 03:28 AM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1