Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ITEC 380 Organization of programming languages Lecture 4 – Functional Programming."— Presentation transcript:

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

2 LISP Review Lisp Global variables Local variables Functions Operations Lists –car/cdr

3 LISP Objectives Misc. features Mapping lists Association Graph theory

4 LISP 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 LISP 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? (defun controller () (loop (print (eval (read)))) )

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

7 LISP 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 LISP Association List (defparameter *orders* '((John (iPhone AppleTV Keyboard)) (Jim (HTC-One Roku)) (Jill (Speakers Printer)) )

9 LISP 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 LISP Graph theory Method for storing / working with connected information Vertex / Vertices Edges Covers many different RL scenarios OO implementation Lisp implementation

11 LISP 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 LISP 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 LISP 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 LISP 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 LISP 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 LISP 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 LISP 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 LISP Flexibility Calculator program Unknown operations ahead of time –Operation –Numbers –Result? OO implementation Lisp implementation –(eval list)

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

20 LISP Next week Functional programming - Lisp


Download ppt "ITEC 380 Organization of programming languages Lecture 4 – Functional Programming."

Similar presentations


Ads by Google