190 likes | 208 Views
Functional Programming: Lisp. MacLennan Chapter 10. Control Structures. Atoms are the only primitives: since they are the only constructs that do not alter the control flow. Literals (represent themselves) Numbers Quoted atoms lists Unquoted atoms are bound to
E N D
Functional Programming: Lisp MacLennan Chapter 10
Control Structures • Atoms are the only primitives: since they are the only constructs that do not alter the control flow. • Literals (represent themselves) • Numbers • Quoted atoms • lists • Unquoted atoms are bound to • Functions (if they have expr property) • Data values (if they have apval property) • Control-structure constructors • Conditional expression • Recursive application of a function to its arguments
In LISP we have conditional expression. • While in Fortran and Pascal we have to drop from expression level to instruction level to make a choice.
The logical connectives are evaluated conditionally • (or x y) = (if x t y) • (or (eq (car L) ‘key) (null L) ) • What happen if L is null? • (or (null L) (eq(car L) ‘key) ) • Does this work? • (if (null L) t (eq (car L) ‘key))
Iteration is done by recursive (defun getprop (p x) (if (eq (car x) p) ( cadr x) (getprop p (cddr x)) ))
Reduction : reducing a list to one value (defun plus-red (a) (if (null a) 0 (plus (car a) (plus-red (cdr a)) )) ) (plus-red ‘(1 2 3 4 5)) >> 15
Mapping: mapping a list into another list of the same size (defun add1-map (a) (if (null a) nil (cons (add1 (car a)) (add1-map (cdr a)) )) ) (add1-map ‘(1 9 8 4)) >>(2 10 9 5)
Filtering: forming a sublist containing all the elements that satisfy some property (defun minus-fil (a) (cond ((null a) nil) (( minusp (car a)) (cons (car a) minusp-fil (cdr a)) )) (t (minusp-fil (cdr a)) )) )
Some thing like Cartesian product , which needs two nested loop • For example: (All-pairs ‘(a b c) ‘(x y z)) >>((a x) (a y) (a z) (b x) (b y) (b z) (c x) (c y) (c z)) We use distl (distribute from left) (distl ‘b ‘(x y z)) >>((b x) (b y) (b z))
(defun all-pairs (M N) (if (null M) nil (append (distl (car M) N) (all-pairs (cdr M) N)) )) (defun distl (x N) (if (null N) nil (cons (list x) (car N)) (distl x (cdr N)) )) ) (the list function makes a list out of its argument)
Recursion for Hierarchical structures (defun equal (x y) ( or (and (atom x) (atom y) (eq x y)) (and (not (atom x)) (not (atom y)) (equal (car x) (car y)) (equal (cdr x) (cdr y)) )) )
Functional arguments allow abstraction • mapcar: a function which applies a given function to each element of a list and returns a list of the results. (defun mapcar (f x) (if (null x) nil (cons (f (car x)) (mapcar f (cdr x)) )) ) (mapcar ‘add1 ‘(1 9 8 4)) >> (2 10 9 5) (mapcar ‘zerop ‘(4 7 0 3 -2 0 1)) >> (nil nil t nil nil t nil) (mapcar ‘not (mapcar ‘zerop ‘(4 7 0 3 -2 0 1))) >> (t t nil t t nil t)
Functional arguments allow programs to be combined • They simplify the combination of already implemented programs.
Lambda expressions are anonymous functions • Instead of defining a name for a function (defun twotimes (x) (times x 2) (mapcar ‘twotimes L) • We may write: (mapcar ‘(lambda (x) (times x 2)) L)
Name Structures • The primitive name structures are the individual bindings of names (atoms) to their values. • Bindings are established through • Property lists • By pseudo-functions • set (like declaring a variable) • Defun (like declaring a procedure) • Actual-formal correspondence
Temporary bindings are a simple, syntactic extension (with let )
Dynamics scoping is the constructor • Dynamic scoping complicates functional arguments