Question

I am currently learning scheme and I came across these functions:

(define t (lambda (x) (lambda (y) x))) 
(define f (lambda (x) (lambda (y) y))) 

Apparently they are representations of true and false as functions. I have no idea why!

I have two questions:

1) What do the successive lambdas mean? I am only used to seeing a single lambda which is used to pass arguments to a function; i.e.

(define add
  (lambda (x y)
    (+ x y)))

And by calling (add 1 5) I would be provided with 6 as output.

2) How would these true and false functions be used?

Was it helpful?

Solution

What is happening here is something known as currying - transforming a function which takes n multiple arguments in such a way that it can be called as a chain of functions

Let us consider a function f which takes 2 arguments, i.e. f(x,y). There exists a unary function g such that f(x,y) = g(x)(y) =(g(x))(y). The function g is known as the curried version of f.

g is a function which expects one argument, (x) and the value of g(x) is also a function of one argument, y.

Let us consider a curried-add function:

(define curried-add 
  (lambda (x) 
    (lambda (y) (+ x y))))

((curried-add 1) 5)

The call to (curried-add 1) would return a function which takes one argument, in our case 5 and adds it to 1, giving and output of 6.

We can chain these curried-adds together to get:

((curried-add ((curried-add 1) 2)) 3)

Would produce an output of 6. This is because (curried-add 1) would return a function expecting one argument, in this case 2. Therefore 1 is added to 2 and produces a function which is expecting one argument which can be added to the 3 we've just made.

In this case of your true and false functions.

True is : (define t (lambda (x) (lambda (y) x)))

False is: (define f (lambda (x) (lambda (y) y)))

The true function takes two arguments and returns the first one, the false function returns the second of the two arguments.

OTHER TIPS

As @Hayden pointed out in his answer, the successive lambdas are an example of currying, in essence is just a function that returns another function:

In mathematics and computer science, currying is the technique of transforming a function that takes n multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions, each with a single argument (partial application). It was originated by Moses Schönfinkel and later re-discovered by Haskell Curry

For the second part of your question: boolean values can be encoded as functions, it's a representation of truth values using Church booleans in lambda calculus, see the links to understand how they're used:

Church booleans are the Church encoding of the boolean values true and false. Some programming languages use these as an implementation model for boolean arithmetic; examples are Smalltalk and Pico. The boolean values are represented as functions of two values that evaluate to one or the other of their arguments. Formal definition in lambda calculus:

true ≡ λa.λb. a
false ≡ λa.λb. b

They're functions, which when called with 1 argument returns another function. So you could call them like this -

(define v (some expression that returns t or f))
((v 'foo) 'bar) ; ==> foo if v is t, bar if v is f

This is sort of like (if v 'foo 'bar) with normal built-in Booleans. It's often used to encode true/false in bare-bones lambda calculus.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top