















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Lecture notes and quiz questions from a scheme programming course. Topics covered include list operations such as length, list, append, and reverse, assignment with set!, set-car!, and set-cdr!, loops with do and for-each, and evaluation order in scheme. The document also includes examples and explanations of applicative-order and normal-order evaluation.
Typology: Quizzes
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















Lecture - XIVMarch 7
th, 2006
Consider the following Scheme definition:
(define (test x y) (if (= x 3) 1 y))Write the output of the following call to this function>> (test 3 (/ 5 0))a) assuming normal-order evaluation is usedb) assuming applicative-order evaluation is used
Scheme^ –
List Operations
-^
Assignment
-^
Loops
-^
Evaluation Order in Scheme
Lambda Calculus
-^
Comments on Midterm Exam
Scheme provides a variety of procedures for operating on lists: •^
length
takes one argument (a list) and returns an integer giving the
length of the list.^ >> (length '(0 #t #f))^3
-^
list
takes one or more arguments and constructs a list of those items.>> (list 1)
; == (cons 1 `())
(1)>> (list 1 2 3)(1 2 3)
reverse
takes one list, and returns a new list with the
same elements in the opposite order.>> reverse '(1 2 3 4))(4 3 2 1)
-^
set!
-^
set-car!
-^
set-cdr!
(let ((x 2)
(l (a b)))(set x! 3)(set-car! l(c d))(set-cdr! l `(e))
x 3 >>l((c d) e)
for-each
(lambda (a b) (display (* a b)) (newline))
(2 4 6)(3 5 7))Scheme uses
applicative-order
evaluation
-^
Evaluate arguments before passing them to subroutine
(define double (lambda (x) (+ x x))) Applicative order evaluation (as in Scheme): >> (double (* 3 4))
(double 12)
-^
In specific cases, the outcome can be different. Eg. (define switch (lamda x a b c)
(cond
((< x 0) a)((= x 0) b)((> x 0) c))))
Using applicative order: >> (switch -1 (+ 1 2) (+ 2 3) (+ 3 4))
(switch -1 3 (+ 2 3) (+ 3 4))
(switch -1 3 5 (+ 3 4))
(switch -1 3 5 7)
(cond
((< x 0) 3)((= x 0) 5)((> x 0) 7))
(cond
(#t 3)((= x 0) 5)((> x 0) 7))
Using normal order: >> (switch -1 (+ 1 2) (+ 2 3) (+ 3 4))
(switch -1 (+ 1 2) (+ 2 3) (+ 3 4))
(cond
((< x 0) (+ 1 2))((= x 0) (+ 2 3))((> x 0) (+ 3 4)))
(cond
(#t (+ 1 2))((= x 0) (+ 2 3))((> x 0) (+ 3 4)))
(+ 1 2)
3
Normal order avoids evaluating (+ 2 3) and (+ 3 4)
lambda calculus
is a
formal system
designed to
investigate
function
definition, function application,
and
recursion
was introduced in 1930’s
-^
the smallest universal programming language
-^
lambda calculus consists of a single transformation rule(variable substitution) and a single function definitionscheme
-^
lambda calculus is universal in the sense that anycomputable function can be expressed and evaluatedusing this formalism
In lambda calculus, every
expression
stands for a
function with a single argument
-^
the
argument
of the function is in turn a function with
a single argument
-^
and the
value
of the function is another function with a
single argument Eg. f(
x) =
x
would be expressed as
λ
x
x^
and the number
f
would be written as
(λ
x
x^
Consider a function which takes another function asargument and applies it to the argument 3:
λ
f
f^
This latter function could be applied to our earlier"add-two" function as follows:
(λ
f
f^
x
x+2)
(λ
f
f^
x
x^
= (λ
x
x^
Formal definition: The set of all lambda expressions can then be described
by the following
context-free grammar
in
expr
identifier
expr
λ identifier. expr
expr
expr expr