Functional ProgrammingIn this set of notes you will learn about:
|
Note: this set of notes covers Chapter 11 Sections 11.1 to 11.2, except Sections 11.2.2, 11.2.4, and 11.2.5 which you are not required to study |
Why Functional Programming?
|
Historical Origins of Functional Programming
|
||||||||||||||
Functional Programming
|
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 xi. To exit Scheme, type (exit). To download the Scheme program "Eliza", click here. |
Data Structures
'( elt1 elt2 elt3 ... ) |
List Operations
|
Type Checking
|
If-Then-Else
(if condition thenexpr elseexp) (cond listofconditionvaluepairs) where the condition value pairs is a list of (cond value) and the condition of the last pair can be else to return a default value |
Testing
|
Lambda Expressions
(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
(function arg1 arg2 arg3 ... ) where function can be a lambda expression |
Functions
(define name function) (define sqr (lambda (x) (* x x)) ) defines function sqr (lambda (a b) (sqrt (+ (* a a) (* b b))) ) ) defines function hypot |
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 ) ) ) |
Bindings
(let listofnameandvaluepairs expression) where name and value pairs 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 listofnameandvaluepairs expression) where name and value pairs 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 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 ... ) ) |
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 ) ) ) |
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
|