170 likes | 336 Views
ITEC 380. Organization of programming languages Lecture 5 – Functional Programming. Review. List manipulation 2.0 Association A touch of graph theory Map / reduce Homework Questions?. Objectives. In class coding Lambda Loops Lazyness Structures/Classes. In-class coding. Problems
E N D
ITEC 380 Organization of programming languages Lecture 5 – Functional Programming
Review • List manipulation 2.0 • Association • A touch of graph theory • Map / reduce • Homework • Questions?
Objectives • In class coding • Lambda • Loops • Lazyness • Structures/Classes
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
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()); } }
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))
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)
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*)
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) )
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) )
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))
Structures • Can group information together • Example (defstruct triangle (base 5) (altitude 5)) (setq test (make-triangle :base 7 :altitude 8)) (triangle-base test)
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))) (setqFPoint (make-instance 'point)) (setf (slot-value FPoint 'x) 10) (slot-value FPoint 'x)
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
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!
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
Next week • Last week of functional programming – Lisp • Logical / declarative programming coming up