1 / 20

ITEC 380

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

braima
Download Presentation

ITEC 380

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITEC 380 Organization of programming languages Lecture 4 – Functional Programming

  2. Review • Lisp • Global variables • Local variables • Functions • Operations • Lists • car/cdr

  3. Objectives • Misc. features • Mapping lists • Association • Graph theory

  4. 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*)

  5. 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)))) )

  6. Exercise • Collabedit.com • Read in two numbers • Add them together • Print out big if > 6 • Print out small otherwise

  7. 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

  8. Association List (defparameter*orders* '((John (iPhone AppleTV Keyboard)) (Jim (HTC-One Roku)) (Jill (Speakers Printer)) ) )

  9. 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)) )

  10. Graph theory • Method for storing / working with connected information • Vertex / Vertices • Edges • Covers many different RL scenarios • OO implementation • Lisp implementation

  11. 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)) ) )

  12. 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.))

  13. 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?

  14. 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)))))

  15. 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))

  16. 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

  17. 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)))

  18. Flexibility • Calculator program • Unknown operations ahead of time • Operation • Numbers • Result? • OO implementation • Lisp implementation • (eval list)

  19. Code example • From the book • Small to large • How it all fits together http://landoflisp.com/wizards_game.lisp

  20. Next week • Functional programming - Lisp

More Related