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

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

Pre 2010

Uploaded on 08/19/2009

koofers-user-ibc
koofers-user-ibc 🇺🇸

10 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Programming Languages
(CS 550)
Lecture 4 Summary
Functional Programming and Operational
Semantics for Scheme
Jeremy R. Johnson
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

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

Outline

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)

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 tosubstituting the argument expressions intothe 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 thesubstitution model falls apart and a differentmodel, more complicated model, must beused.

Environments used to store variables andbindings.

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

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

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 Prime? +, a,b

Streams are Not Lists

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