Docsity

Functional Programming and Operational Semantics for Scheme: Lecture 4 Summary - Prof. Jer, Papers of Programming Languages

Partial preview of the text

Download Functional Programming and Operational Semantics for Scheme: Lecture 4 Summary - Prof. Jer and more Papers Programming Languages in PDF only on Docsity!

Programming Languages

(CS 550)

Lecture 4 Summary

Functional Programming and Operational

Semantics for Scheme

Jeremy R. Johnson

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.

Outline

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

Outline

Substitution model of computation

Modeling state with assignment

Environments

Streams and delayed evaluation

Scheme interpreter (meta-circular)

Higher Order Functions

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

function application corresponds to

substituting the argument expressions into

the formal parameters of the function body

Order of evaluation

Applicative vs. normal order lambda calculus Church-Rosser

Order Matters

(define (p) (p))

(define (test x y)

(if (= x 0) 0 y))

(test 0 (p))

Environments

When assignment is introduced the

substitution model falls apart and a different

model, more complicated model, must be

used.

Environments used to store variables and

bindings.

Values can change assignment supported List of frames to support nested scope

Modeling State with Assignment

(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

Cost of Assignment

(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

Streams

Sequence of elements

(cons-stream x y) (stream-car s) (stream-cdr s)

(stream-null? s) the-empty-stream

Streams

(define (sum-primes a b) (accumulate + 0 (filter prime? (enumerate-interval a b))))

Enumerate filter accumulate

a,b Prime? +,

Streams are Not Lists

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