The following question is given in our Programming language practice exam and I am having a hard time understating how this works. Could someone tell me what the flow of code is? I have run it in racket and know what the answer is. It looks like the first lambda function is taking the other two functions as argument. But then where are the inputs (lambda (x) 2)
and (lambda (y) 3)
passed to?
(((lambda (x y) (x y))
(lambda (y) (lambda (y x) (x (x y))))
(lambda (x) (lambda (x y) (x (y x)))))
(lambda (x) 2)
(lambda (y) 3))
The answer to the question is 3.
Like all programming languages, Scheme allows us to build our own procedures and add them to the set of existing ones. Very few implementations of Scheme even distinguish between the built-in functions and the user-defined ones. symbol and a set of parameters with a parameterized expression.
A function call is written as (f args) where f is the name of the function and args a space-separated sequence of arguments. So to call tab without arguments, you'd write (tab) and to call translate with the argument x , you'd write (translate x) .
Scheme is a multi-paradigm programming language. It is a dialect of Lisp which supports functional and procedural programming. It was developed by Guy L. Steele and Gerald Jay Sussman in the 1970s. Scheme was introduced to the academic world via a series of papers now referred to as Sussman and Steele's Lambda Papers.
Scheme is primarily a functional programming language. It shares many characteristics with other members of the Lisp programming language family. Scheme's very simple syntax is based on s-expressions, parenthesized lists in which a prefix operator is followed by its arguments.
We humans like to name things. Succinct notation with short names makes it easy to manipulate the code mentally, because so much of our mental abilities are tied into our massively parallel visual recognition system:
(((lambda (x y) (x y))
(lambda (y) (lambda (y x) (x (x y))))
(lambda (x) (lambda (x y) (x (y x)))))
(lambda (x) 2)
(lambda (y) 3)) =>
((u where u = (lambda (x y) (x y))
f f = (lambda (y) (lambda (y x) (x (x y))))
g) g = (lambda (x) (lambda (x y) (x (y x))))
(lambda (x) 2)
(lambda (y) 3)) =>
((u where (u x y) = (x y)
f (f y) = \(y x) -> (x (x y)) ; (*)
g) (g x) = \(x y) -> (x (y x))
(lambda (x) 2)
(lambda (y) 3)) =>
((f where (f g) = \(y x) -> (x (x y))
g) (g x) = \(x y) -> (x (y x))
(lambda (x) 2)
(lambda (y) 3)) =>
(h where h = \(y x) -> (x (x y))
p p = \(x) -> 2
q) => q = \(y) -> 3
(h where (h y x) = (x (x y))
p (p x) = 2
q) => (q y) = 3
(q (q p)) where (p x) = 2
(q y) = 3
=>
(q 3) where (q y) = 3
=>
3
The (*)
definition has all the variables bound in the lambda expression (lambda (y x) (x (x y)))
-- both x
and y
. The argument y
in (f y)
is thus ignored. It would have been referenced by a free variable y
in the lambda expression, but there is none.
This is a job for the algebraic stepper!
Enter this (no #lang line) in the interaction window of DrRacket. In the lower left corner change the language to "Intermediate Student with Lambda". Now click the "Run" button. Finally click the "Stepper" button (the left most button to the left of the run button.
You can now single step through the program (and go back!).
(((lambda (x y) (x y))
(lambda (y) (lambda (y x) (x (x y))))
(lambda (x) (lambda (x y) (x (y x)))))
(lambda (x) 2)
(lambda (y) 3))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With