























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
A summary of lecture 4 in the cs 550 programming languages course, focusing on functional programming and operational semantics for scheme. Topics include the evaluation of scheme expressions, functional programming concepts such as programs as functions, no variables or loops, and functions as first-class objects.
Typology: Papers
1 / 31
This page cannot be seen from the preview
Don't miss anything!
























Starting Point Informal Scheme Semantics To evaluate (E1 E2 ... En), recursively evaluateE1, E2,...,En - E1 should evaluate to a function -and then apply the function value of E1 to thearguments given by the values of E2,...,En. In the base case, there are self evaluatingexpressions (e.g. numbers and symbols). Inaddition, various special forms such as quote andif must be handled separately.
Review scheme syntax and semantics Functional programming ¾ Programs are functions – for every input there is aunique output ¾ No variables ⇒ no assignment and no loops ¾ Use recursion for control ¾ Functions are first class objects Pass as arguments and return as values ¾ Simple semantics [value semantics] (no side effects,referential transparency)
sort: (sort '(4 3 2 1) <) => (1 2 3 4) map: (map sqr '(1 2 3 4)) => (1 4 9 16) filter: (keep-matching-items '(1 2 3 4 5) odd?) => (1 3 5) reduce: (reduce * 1 '(1 2 3 4)) => 24 Functions that return functions ¾ (define (make-adder x) (lambda (y) (+ x y))) ¾ Function composition (define (compose g f) (lambda (x) (g (f x)))) ¾ Currying (define (curry f b) (lambda (a) (f a b)))
Substitution Model of Computation
Applicative vs. normal order ¾ lambda calculus ¾ Church-Rosser
(define (p) (p)) (define (test x y) (if (= x 0) 0 y)) (test 0 (p))
Values can change ¾ assignment supported ¾ List of frames to support nested scope
(define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch m) (cond ((eq? m 'withdraw) withdraw) ((eq? m 'deposit) deposit) (else (error "Unknown request -- MAKE- ACCOUNT" m)))) dispatch) (define acc (make-account 100)) ((acc 'withdraw) 50) 50 ((acc 'withdraw) 60) "Insufficient funds" ((acc 'deposit) 40) 90 ((acc 'withdraw) 60) 30
(define (make-simplified-withdraw balance) (lambda (amount) (set! balance (- balance amount)) balance)) (define W (make-simplified-withdraw 25)) (W 10) 15 (W 10) 5 (define (make-decrementer balance) (lambda (amount) (- balance amount))) (define D (make-decrementer 25)) (D 10) 15 (D 10) 15
Environment Model Solves Problem (define W1 (make-withdraw 100)) (W1 50) (define W2 (make-withdraw 100)) Global Env Balance = 50 Balance = 100 E E Parameters body W W Make-withdraw
(cons-stream x y) ¾ (stream-car s) ¾ (stream-cdr s) ¾ (stream-null? s) ¾ the-empty-stream
(define (sum-primes a b) (accumulate + 0 (filter prime? (enumerate-interval a b)))) Enumerate filter accumulate Prime? +, a,b
Consider the following example (car (cdr (filter prime? (enumerate-interval 10000 1000000)))) This would be extremely inefficient ifimplemented with lists ¾ Do not build the entire stream of elements ¾ Get the next element from the stream when needed ¾ Necessary for potentially infinite streams