200 likes | 387 Views
ITEC 380. Organization of programming languages Lecture 4 – Functional Programming. Review. Lisp Global variables Local variables Functions Operations Lists c ar/ cdr. Objectives. Misc. features Mapping lists Association Graph theory. Lists. Adding to a list
E N D
ITEC 380 Organization of programming languages Lecture 4 – Functional Programming
Review • Lisp • Global variables • Local variables • Functions • Operations • Lists • car/cdr
Objectives • Misc. features • Mapping lists • Association • Graph theory
Lists • Adding to a list • Not the only method for this • What typically goes with pushing? • What can you do with the value that comes out of the list? (defparameter *exampleList* ‘(7 8 9)) (push 1 *exampleList*)
Looping • Desire • Create a command line simulator that will execute whatever function you type in and ask for another • Code • Similarity to the shell • Why is this useful compared to the shell? • What security risks does this produce? (defuncontroller () (loop (print (eval (read)))) )
Exercise • Collabedit.com • Read in two numbers • Add them together • Print out big if > 6 • Print out small otherwise
Data storage • Database / key store • Orders example • How would we store this in Lisp? • Several variables? • Lists? John – iPod AppleTV Keyboard Jim – HTC-One Roku Jill – Speakers Printer
Association List (defparameter*orders* '((John (iPhone AppleTV Keyboard)) (Jim (HTC-One Roku)) (Jill (Speakers Printer)) ) )
Accessing a single order • Two lines of code • How would we store this in an OO language? • How would we retrieve it in an OO language • Collabedit • Comparison (defun get-order(person database) (cadr (assoc person database)) )
Graph theory • Method for storing / working with connected information • Vertex / Vertices • Edges • Covers many different RL scenarios • OO implementation • Lisp implementation
Maps • Consider the previous problem except with countries and cities • Need to know which city can be gotten to from another city (defparameter*places* '((Virginia (Blacksburg Radford Floyd)) (Tennessee (JohnsonCity Bristol Kingsport)) (NorthCarolina (Boone Lenoir Raleigh)) ) )
Graph • Connect each place to another • Basic graph theory (defparameter *edges* '((Virginia (NorthCarolina South Blacksburg) (Tennessee East Bristol)) (NorthCarolina (Virginia South Radford)) )) (defun describe-path (edge) `(there is a ,(caddr edge) going ,(cadr edge) from here.))
Mapping • Apply an action to each item in a list • (mapcar #’sqrt ‘(6 7 8 9) ) • Can use as a simple car/cdr or whatever function you want • What procedural / OO feature does this replace?
Book example • Rooms in a house • Pathways between rooms • This function will go through and list the pathways for each location • Demonstrates the power of lisp (defun describe-paths (location edges) (apply #'append (mapcar #'describe-path (cdr(assoc location edges)))))
Reducing • Mapping allows us to select / manipulate large quantities of data • Reduce is a function that takes a list of data and performs the operation / function on the members of the list • Can specify where to start • Code example (reduce #’+ ‘(5 6 7 8))
Map + reduce • Simple concept • Large impact • Industry examples • Farm each particular function out to multiple machines • Collect the results on a specified machine with reduce • Google / amazon use it
Apply • What if you need to pass each value in a list to a function? • Usages • Constructing a single list out of smaller parts • Almost identical to reduce, though the way results are formed is a bit different (reduce pairs them, apply puts them in a single list) (apply #’append ‘((list one) (two) (three four)))
Flexibility • Calculator program • Unknown operations ahead of time • Operation • Numbers • Result? • OO implementation • Lisp implementation • (eval list)
Code example • From the book • Small to large • How it all fits together http://landoflisp.com/wizards_game.lisp
Next week • Functional programming - Lisp