Scheme Programming: Quiz & Lecture Notes on List Ops, Assignment, Loops, & Eval Order, Quizzes of Programming Languages

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

Pre 2010

Uploaded on 08/31/2009

koofers-user-if9
koofers-user-if9 🇺🇸

10 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Programming Languages
Tevfik Koşar
Lecture - XIV
March 7th, 2006
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Scheme Programming: Quiz & Lecture Notes on List Ops, Assignment, Loops, & Eval Order and more Quizzes Programming Languages in PDF only on Docsity!

Programming Languages

Tevfik Koşar

Lecture - XIVMarch 7

th, 2006

Quiz - 2

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

Roadmap

•^

Scheme^ –

List Operations

-^

Assignment

-^

Loops

-^

Evaluation Order in Scheme

•^

Lambda Calculus

-^

Comments on Midterm Exam

List Operations

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)

List Operations

•^

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)

Assignment

-^

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)

Loops

for-each

(lambda (a b) (display (* a b)) (newline))

(2 4 6)(3 5 7))

Evaluation Order in Scheme

•^

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) 

Evaluation Order in Scheme

-^

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))

Evaluation Order in Scheme

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

•^

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

Lambda Calculus

•^

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^

Lambda Calculus

•^

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^

Lambda Calculus

Formal definition: The set of all lambda expressions can then be described

by the following

context-free grammar

in

BNF

expr

identifier

expr

λ identifier. expr

expr

expr expr