ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.
-
Upload
ruby-morton -
Category
Documents
-
view
222 -
download
4
Transcript of ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.
![Page 1: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/1.jpg)
ITEC 380
Organization of programming languages
Lecture 5 – Functional Programming
![Page 2: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/2.jpg)
LISP
Review
• List manipulation 2.0• Association• A touch of graph theory• Map / reduce• Homework– Questions?
![Page 3: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/3.jpg)
LISP
Objectives
• In class coding• Lambda• Loops• Lazyness• Structures/Classes
![Page 4: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/4.jpg)
LISP
In-class coding
• Problems– Given a list, return the average if all of
the contents are integers, or return nil if there is a non-integer in the list
– Given a list of items as a parameter to your function, return true if there are any duplicates, nil other wise
![Page 5: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/5.jpg)
LISP
Lambda
• OO version– Inner classes– Example
class AnExample{
public void function(){
class MyActionListener : ActionListener{
public void actionPerformed(){}}JButton clicker = new Jbutton();clicker.setActionListener(new MyActionListener());
}}
![Page 6: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/6.jpg)
LISP
Lambda
• Lisp version
• Allows you to define a function wherever you need to use one
• What are the pros / cons of this method?
(lambda (n) (n/2))
(mapcar (lambda (n) (/ n 2)) '(8 10 12))
![Page 7: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/7.jpg)
LISP
Looping
• Non-functional type command• Allows for typical for loop tasks, uses different syntax
• For creates a local variable and iterates through a list• Stopped by sentinel named below• Sum is the usual• Other tricks
– For I from 5 to 10 – For I in ‘(1 2 3)– For I below 5 do (function)– For I below 10 when (condition) sum I – Collect (operation) //Produces a list
(loop for i below 5 sum i)
![Page 8: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/8.jpg)
LISP
Lazyness
• Issue–Whenever lisp gets information, it
evaluates it– Given a lisp program that can play
chess, how long will it take to handle a move if all possible moves are analyzed?
– Example (defun sub (x y)
(princ “Calculation”)(- x y)
)(defparameter *test* (lazy (sub 5 3))(force *foo*)
![Page 9: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/9.jpg)
LISP
Problem
• Not part of common lisp, so how do we create it?
• Macros – Unevaluated lisp code that is returned in proper form
• Example(defmacro Square(x)
‘(* X X))
![Page 10: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/10.jpg)
LISP
Why?
• Compile vs coding time• Example of the when implementation
(defmacro when (condition &rest body) `(if ,condition (progn ,@body)))
(when (> 5 3)(princ ‘5 is greater than 3)
)
![Page 11: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/11.jpg)
LISP
End-result
• Code(defmacro lazy (&body body)
(let ((forced (gensym)) (value (gensym))) `(let ((,forced nil) (,value nil)) (lambda ()
(unless ,forced (setf ,value (progn ,@body)) (setf ,forced t))
,value)))) (defun force (lazy-value) (funcall lazy-value))
![Page 12: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/12.jpg)
LISP
Structures
• Can group information together• Example
(defstruct triangle (base 5) (altitude 5))(setq test (make-triangle :base 7 :altitude 8))(triangle-base test)
![Page 13: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/13.jpg)
LISP
Collections
• How do you wed OO / non OO together?
• What are structures in C/C++ or records in Ada?
• What are the benefits / downsides of them?
• Lisp equivalent (defclass point() ((x :type number) (y :type number)))(setq FPoint (make-instance 'point))(setf (slot-value FPoint 'x) 10)(slot-value FPoint 'x)
![Page 14: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/14.jpg)
LISP
Other Features
• Constructor with default values– :initform value after the var name in defclass
• Accessibility– :reader names the function that will read the
slot– :write names the function that will write to
the slot
• Singletons– :allocation :class
• Also allows for data inheritance
![Page 15: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/15.jpg)
LISP
Questions
• What do you think of the structure / OO version in Lisp?
• Why is it there?• What is the purpose / problem it is
trying to solve?• The underlying issue!
![Page 16: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/16.jpg)
LISP
Functional Programming
• 2 major sections• Pure functional– Functions that access no outside
variables– Can be run in parallel
• Parts that cause side effects– Variables that hold state– Interact with the outside world
![Page 17: ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.](https://reader036.fdocuments.in/reader036/viewer/2022083005/56649f0d5503460f94c21f60/html5/thumbnails/17.jpg)
LISP
Next week
• Last week of functional programming – Lisp
• Logical / declarative programming coming up