Download presentation
Presentation is loading. Please wait.
Published byRuby Morton Modified over 9 years ago
1
ITEC 380 Organization of programming languages Lecture 5 – Functional Programming
2
LISP Review List manipulation 2.0 Association A touch of graph theory Map / reduce Homework –Questions?
3
LISP Objectives In class coding Lambda Loops Lazyness Structures/Classes
4
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
5
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()); }
6
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))
7
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)
8
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*)
9
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) )
10
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) )
11
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))
12
LISP Structures Can group information together Example (defstruct triangle (base 5) (altitude 5)) (setq test (make-triangle :base 7 :altitude 8)) (triangle-base test)
13
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)
14
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
15
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!
16
LISP Functional Programmin g 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
17
LISP Next week Last week of functional programming – Lisp Logical / declarative programming coming up
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.