| 2. Functional Programming |
Overview
|
Note: Study Chapter 11 Sections 11.1 to 11.2, except Sections 11.2.2, 11.2.4, and 11.2.5. |
Concepts of Functional Programming
|
Lisp
|
A Crash Course on Scheme
(function 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 listofconditionvaluepairs) where the condition value pairs is a list 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 formal parameters are the function inputs and the function body 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 listofnameandvaluepairs expression) where name and value pairs is a list of pairs (namevalue) 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 listofnameandvaluepairs expression) where name and value pairs is a list of pairs (namevalue) 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 condition body) (do ((i 0 (+ i 1))) ((>= i 10) "done") (display i) (newline) ) Since 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))) ) ) ) |
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. |