

























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 programming languages (cs 550) course, focusing on functional programming and operational semantics for scheme. Topics include the starting point of informal scheme semantics, the theme of programs as functions and their semantics, functional programming concepts like programs as functions, no variables, recursion, and functions as first-class objects, and an outline of the topics to be covered in the lecture.
Typology: Papers
1 / 33
This page cannot be seen from the preview
Don't miss anything!


























Starting Point
Informal Scheme Semantics
To evaluate (E1 E2 ... En), recursively evaluate
E1, E2,...,En - E1 should evaluate to a function - and then apply the function value of E1 to the arguments given by the values of E2,...,En.
In the base case, there are self evaluating
expressions (e.g. numbers and symbols). In addition, various special forms such as quote and if must be handled separately.
Review scheme syntax and semantics
Functional programming
Programs are functions – for every input there is a unique 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
14
(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)
16
Environment Model Solves Problem
(define W1 (make-withdraw 100)) (W1 50) (define W2 (make-withdraw 100))
Global Env
E1 (^) Balance = 50 E2 Balance = 100
Parameters body
W1 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
a,b Prime? +,
Consider the following example
(car (cdr (filter prime? (enumerate-interval 10000 1000000))))
This would be extremely inefficient if
implemented with lists Do not build the entire stream of elements Get the next element from the stream when needed Necessary for potentially infinite streams