History Functional Programming Based on Prof. Gotshalks notes on functionals 1977 Turing 1 Lecture John Backus described functional programming The problem with current languages is that they are word-at-a-time 2 > Notable exceptions then were Lisp and APL > Now ML 1 Turing award is the Nobel prize of computer science. 2 'Word-at-a-time' translates to 'byte-at-a-time' in modern jargon. A word typically held 2 to 8 bytes depending upon the type of computer. F2-1 F2-2 Meaningful Units of Work Work with operarations meaningful to the application, not to the underlying hardware & software» Analogy with word processing is not to work with characters and arrays or lists of characters» But work with words, paragraphs, sections, chapters and even books at a time, as appropriate. FP form Note the following properties of functional programs» NO explicit loops ( or recursion)» NO sequencing at a low level» NO local variables In addition, functional programs have the following properties» functions as input» functions as output > In FP frequently write functions that produce a new function using other functions as input F2-3 F2-4
Library of functions Depending upon the application area functions are created.» For example trans transpose a matrix Some are created using existing functionals» For example innerproduct Library of functions 2 Others are created outside of the system for efficiency reasons» For example trans may be more efficient to implement outside of Lisp Although as compiler knowledge grows compilers produce more efficient code than 'coding by hand' Machine speeds increase so many functions execute fast enough The files prism:/cs/course/functionals.lsp and prism:/cs/course/functionalsbase.lsp created by Prof. Gotshalks contain additional library functions F2-5 F2-6 Binding function - bu - 1 Given a binary function it is often useful to bind the first parameter to a constant creating a unary function > Also called currying after the mathematician Curry who developed the idea» (bu + 3) creates a unary add 3 from the binary function + (mapcar (bu + 3) (1 2 3)) ==> (4 5 6)» Cons x before every item in a list (mapcar (bu cons x) (1 2 3)) ==> ((x.1) (x.2) (x.3))» Note that mapcar expects a function definition as the second argument, so we use bu to help construct the function Binding function - bu - 2 We define the function bu as follows: (defun bu ( f x ) (function (lambda ( y ) ( funcall f x y ) ) ) ) F2-7 F2-8
Binding function - bu - 3 We could define the function 3+ ( define 3+ ( x ) ( + 3 x ) )» and use (mapcar 3+ (1 2 3)) ==> (4 5 6)» but this adds to our name space For use-once functions we can use lambda expressions (mapcar # (lambda (x) (+ 3 x)) (1 2 3)) ==> (4 5 6) (mapcar (function ( lambda (x) (+ 3 x) ) ) (1 2 3)) ==> (4 5 6) Binding function - bu - 4 The previous slide solutions are seen as being clumsy and more difficult to read compared to the following bu has a clear meaning with the above you have to reverse engineer to understand (mapcar (bu + 3) (1 2 3)) ==> (4 5 6) Can define functions using bu (defun 3+ (y) (funcall (bu + 3) y)) In such cases we would write (defun 3+ (y) (+ 3 y)) We do not normally use bu to define named functions F2-9 F2-10 The Functional rev rev reverse the order of the arguments of a binary function (defun rev (f) # (lambda (x y) (funcall f y x)) ) Earlier we wrote (mapcar (bu cons x) (1 2 3)) ==> ((x.1) (x.2) (x.3)) Suppose we want ((1.x) (2.x) (3.x)) then we write (mapcar (bu (rev cons) x) (1 2 3)) ==> ((1.x) (2.x) (3.x)) Other Functionals in the file - 1 In prism:/cs/course/3401/functionals.lsp the following functionals are described (comp unaryfunction1 unaryfunction2) > Compose two unary functions (compl unaryfunction1 unaryfunction2... unaryfunctionn) > Compose a list of unary functions (trans matrix) > Transpose a matrix F2-11 F2-12
Other Functionals in the files - 2 (distl anitem thelist) > Distribute anitem to the left of items in thelist (distl a (1 2 3)) ==> ((a 1) (a 2) (a 3)) (distr anitem thelist) > Distribute anitem to the right of items in thelist (distr a (1 2 3)) ==> ((1 a) (2 a) (3 a)) functional examples > (mapcar ( bu 'member 'y ) '( ( y ) ( ) ( a b) ( y x ) ) ) ( ( Y ) NIL NIL ( Y X ) ) filter - filters a list keeping items that satisfy a predicate > ( filter 'atom '( a () (a b) (x) c ) ) ( A NIL C ) There are many MANY more functions in both functionals.lsp and functionalsbase.lsp look at your leisure!! comp - defines a function as being the composition of a pair of unary functions > ( funcall (comp 'sqrt 'abs ) -9 ) 3.0 F2-13 F2-14 Another functional example - 1 Another functional example - 2 Problem: > ( allpairs (1 2) (x) ) Write a fully functional (no recursion) function, allpairs, ( ( 1 x ) ( 2 x ) ) that given two lists, list1 and list2, returns a list of lists. Each list has 2 elements, 1 from list1 and 1 from list2. All possible pairs are included. ( ( A 1 ) ( B 1 ) ( C 1 ) ( A 2 ) ( B 2 ) ( C 2 ) ( A 3 ) ( B 3 ) ( C 3 ) ) F2-15 F2-16
Another functional example - 3 Another functional example - 4 From functionals.lsp: Distribute right distributes the first parameter to the right of Each of the elements in the second parameter. distr - functional 2 parameter version distr1 functional 1 parameter version > (distr a ( 1 2 3 ) ) ( ( 1 A ) ( 2 A ) ( 3 A ) ) > (distr1 (a ( 1 2 3 ) ) ) ( ( 1 A ) ( 2 A ) ( 3 A ) ) (???? ) F2-17 F2-18 Another functional example - 5 Another functional example - 6 ( distr1 ( list a b ) ) ) ( mapcar distr1 (distr1 ( list a b ) ) ) ) ( ( 1 ( A B C ) ) ( 2 ( A B C ) ) ( 3 ( A B C ) ) ) ( ( ( A 1 ) ( B 1 ) ( C 1 ) ( ( A 2 ) ( B 2 ) ( C 2 ) ) ( ( A 3 ) ( B 3 ) ( C 3 ) ) ) F2-19 F2-20
Another functional example - 7 Yet another functional example - 1 ( apply 'append ( mapcar 'distr1 (distr1 ( list a b ) ) ) ) Generate the list (1 3 9 27 81). ) ( ( A 1 ) ( B 1 ) ( C 1 ) ( A 2 ) ( B 2 ) ( C 2 ) ( A 3 ) ( B 3 ) ( C 3 ) ) F2-21 F2-22 Yet another functional example - 2 Yet another functional example - 3 Functions for list generation ( genlist length next start ) : generate a list, start is the first item in the list, length is the length of the list and next is the one parameter function that generates the second item. ( range lower upper ) : range gives a list of integers in the range lower to upper inclusive ( genlist 5???? 1 ) Math function ( sigma lower upper func ) : add a function of the integers between lower and upper inclusive F2-23 F2-24
Yet another functional example - 4 ( genlist 5 (bu * 3 ) 1 ) F2-25