| 2. Functional Programming |
Overview
|
Note: Study Chapter 13 Sections 13.1, 13.2, 13.3, 13.5, 13.8, and 13.9. |
Concepts of Functional Programming
|
Lisp
|
A Crash Course on Scheme
(functionName arg1 arg2 arg3 ...) |
Note: You can run the Scheme interpreter and try the examples in these notes by executing the scheme command on the linprog stack (ssh linprog). To exit Scheme, type (exit). You can download an example Scheme program "Eliza". More information on Scheme can be found at http://www.swiss.ai.mit.edu/projects/scheme |
Scheme Data Structures
'(elt1 elt2 elt3 ...) |
Primitive List Operations
|
Type Checking
|
If-Then-Else
(if condition thenExpr elseExpr) (cond conditionValuePairs) where the conditionValuePairs is a sequence of (cond value) pairs and the condition of the last pair can be else to return a default value |
Testing
|
Lambda Abstraction
(lambda formalParameters functionBody) where the formalParameters are the function inputs and the functionBody is an expression that is the resulting value of the function
|
Lambda Application
(function arg1 arg2 arg3 ...) where function can be a lambda abstraction |
Defining Global Functions in Scheme
(define name function) (define sqr (lambda (x) (* x x)) ) defines function sqr (lambda (a b) (sqrt (+ (* a a) (* b b))) ) ) defines function hypot |
Bindings
(let listOfNameValuePairs expression) where listOfNameValuePairs is a list of pairs (name value) and expression is returned in which each name is replaced with its value in the list (let ((a 3) (b 4) ) (hypot a b) ) (let ((sqr (lambda (x) (* x x))) (y 3) ) (sqr y) ) |
Recursive Bindings
(letrec listOfNameValuePairs expression) where listOfNameValuePairs is a list of pairs (name value) and expression is returned where each name is replaced with its value (letrec ((fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1))) ) ) ) ) (fact 5) ) |
I/O and Sequencing
(begin (display "Hello World!") (newline) ) (let ((x 1) (y (read)) (plus +) ) (begin (display (plus x y)) (newline) ) ) |
Loops
(do listOfTriples terminationTest body) (do ((i 0 (+ i 1))) ((>= i 10) "done") (display i) (newline) ) Because everything is an expression in Scheme, a loop must return a value, which in this case is the string "done" |
Higher-Order Functions
(lambda (f n) (f (f n))) ) |
Non-Pure Constructs: Assignments
... (set! a 1) ; overwrite a with 1 ... (begin ... (set! a (+ a 1)) ; increment a by 1 ... ) ) |
Scheme Examples
(define fact (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))) ) ) (define iterfact (lambda (n) (do ((i 1 (+ i 1)) (f 1 (* f i)) ) ((> i n) f) ; note: loop body is omitted ) ) ) |
Example Recursive Functions on Lists
(define sum (lambda (lst) (if (null? lst) 0 (+ (car lst) (sum (cdr lst))) ; add value of head to sum of rest of list ) ) ) (define in? (lambda (elt lst) (cond ((null? lst) #f) ; if list is empty, return false ((= elt (car lst)) #t) ; if element is the head, return true (else (in? elt (cdr lst))) ; keep searching rest of list ) ) ) |
Examples of List Functions
(lambda (num elt) (cond ((= 0 num) '()) (else (cons elt (fill (- num 1) elt))) ) ) ) (lambda (start end) (if (> start end) '() (cons start (between (+ start 1) end)) ) ) ) (lambda (lst1 lst2) (cond ((null? lst1) '()) ((null? lst2) '()) (else (cons (list (car lst1) (car lst2)) (zip (cdr lst1) (cdr lst2)))) ) ) ) (lambda (num lis) (cond ((= num 0) '()) (else (cons (car lis) (take (- num 1) (cdr lis)))) ) ) ) |
Examples of Higher-Order Functions
(define reduce (lambda (op lst) (if (null? (cdr lst)) (car lst) (op (car lst) (reduce op (cdr lst))) ) ) ) (define filter (lambda (op lst) (cond ((null? lst) '()) ((op (car lst)) (cons (car lst) (filter op (cdr lst)))) (else (filter op (cdr lst))) ) ) ) |
Simulating the Actions of a DFA
(lambda (dfa input) (cons (car dfa) ; cons new node (state) followed by list of simulated path nodes (if (null? input) ; no more input? (if (infinal? dfa) '(accept) '(reject)) ; yes: accept or reject (simulate (move dfa (car input)) (cdr input)) ; no: continue ) ) ) ) (define infinal? (lambda (dfa) (memq (car dfa) (caddr dfa)) ; state is final? ) ) (define move (lambda (dfa symbol) (let ((curstate (car dfa)) ; 1st elt in dfa list (trans (cadr dfa)) ; 2nd elt in dfa list (finals (caddr dfa)) ; 3rd elt in dfa list ) (list (if (eq? curstate 'error) ; if in error state then state = error 'error (let ((pair (assoc (list curstate symbol) trans))) (if pair (cadr pair) 'error) ) ) trans ; transition functions (table) finals ; final states ) ) ) ) |
Functional Programming Today
|
Exercise 1: Explain the workings of a Turing Machine. Find information on the Web (for example in the Stanford Encyclopedia of Philosophy). Site your source of information. |