Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions...
-
Upload
shannon-atkin -
Category
Documents
-
view
215 -
download
0
Transcript of Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions...
![Page 1: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/1.jpg)
Functions
Robin Burke
CSC 358/458
4.10.2006
![Page 2: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/2.jpg)
Outline
Errata Homework #2 Project Structures Functions
review parameter lists anonymous fns closures recursion tail recursion
Example
![Page 3: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/3.jpg)
CLOS solution
How many?
![Page 4: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/4.jpg)
Errata
appenddoes consit build a new list for the front partshares structure with the last part
![Page 5: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/5.jpg)
Homework #2
![Page 6: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/6.jpg)
Project
Develop your own Lisp application Not restricted to implementing a game Timeline
4/17 Proposal5/1 Accepted proposal5/22 Progress report6/7 Due
![Page 7: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/7.jpg)
Structures
Annoying to define a pile of accessors Alternative
defstruct Defines a structure
a named typewith slots
![Page 8: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/8.jpg)
defstruct
Definesaccessorsmutatorsconstructorread macro
![Page 9: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/9.jpg)
Example
(defstruct card suit rank) Defines
make-cardcard-suitcard-rankalso#S(CARD :SUIT S :RANK 1)
• will be read and turned into a card object
![Page 10: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/10.jpg)
Functions
Defined with defun, butjust a convenience
Real issuesymbol-function binding
![Page 11: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/11.jpg)
Calling Functions
Normal evaluation(+ 1 2 3)
Othersfuncallapplyavoid eval
![Page 12: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/12.jpg)
Multiple values
Let equivalentmultiple-value-bind
setq equivalentmultiple-value-setq
Example(multiple-value-bind (div rem) (floor num 10)... rest of code ...)
Alsomultiple-value-list
![Page 13: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/13.jpg)
Example
add-digits
![Page 14: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/14.jpg)
Parameter Lists
keywords(defun foo (a &key b c) ... )(foo 5 :b 10 :c 19)(foo 5)
rest(defun foo (&rest lst) ... )(foo)(foo 10 15 25 30)
optional(defun foo (a &optional b c) ... )(foo 5)(foo 5 10 19)
![Page 15: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/15.jpg)
Anonymous Functions
Like anonymous classes in Java but...
lambda a macro that creates function objects(lambda (x) (* x x)
Can create functions where needed useful as functional arguments(mapcar #'(lambda (x) (* x x)) '(1 2 3))
Can write functions to return functional values
![Page 16: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/16.jpg)
Example
numeric series
![Page 17: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/17.jpg)
Closures
Shared lexical variable(defun integers ()
(let ((n 0))
(values #'(lambda () (incf n))
#'(lambda () (setf n 0)))))
Closurebinds variables in external scopekeeps them when the scope
disappears
![Page 18: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/18.jpg)
Environment
Evaluation takes place in an environmentbindings for symbols
Hierarchy of environmentsglobal environmentlocal environments
"Lexical context"
![Page 19: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/19.jpg)
Example
(defun bar (arg1 arg2)
(let ((var1 (baz arg1 arg2)))
#'(lambda () (foo var1))))
(setf fn (bar 'a 'b))
![Page 20: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/20.jpg)
When Closure is Created
Globalenvironment
Function
defun ...defparameter ...
(arg1 arg2)
Let (let ((var1...
Lambda(lambda () (foo var1 ...)
![Page 21: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/21.jpg)
When Closure is Returned
Let (let ((var1...
Lambda(lambda () (foo var1 ...)
Globalenvironment
fn
![Page 22: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/22.jpg)
Encapsulation
Achieves OO goalvariables in closure are totally
inaccessibleno way to refer to their bindingsno way to even access that
environment
![Page 23: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/23.jpg)
Example
Bank account
![Page 24: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/24.jpg)
Compare with Java
Anonymous classes External values only if
instance variablesfinal variables
![Page 25: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/25.jpg)
Anonymous Class (illegal)
JButton okButton = new JButton ("OK"); okButton.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { m_canceled = false; okButton.setEnabled(false); } });
![Page 26: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/26.jpg)
Anonymous Class (legal)
final JButton okButton = new JButton ("OK"); okButton.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { m_canceled = false; okButton.setEnabled(false); } });
// BUT okButton = new JButton ("Yep"); // Illegal
![Page 27: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/27.jpg)
Simulating a Closure in Java
final JButton [] arOkButton = new JButton [1];arOkButton[0] = new JButton ("OK");arOkButton[0].addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) { m_canceled = false; arOkButton[0].setEnabled(false); } });
arOkButton[0] = new JButton ("Yep"); // Legal
![Page 28: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/28.jpg)
Recursion
Basic frameworksolve simplest problem "base case"solve one piececombine with the solution to smaller
problem Why it works
eventually some call = base casethen all partial solutions are combined
![Page 29: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/29.jpg)
Recursive Statement
Some problems have a natural recursive statementmaze followingpattern matching
![Page 30: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/30.jpg)
Example
pattern matching
![Page 31: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/31.jpg)
Double Recursion
Treesoperating on the "current" element
isn't simplerecurse on both car and cdr
Strategybase casecombine solution-to-car with solution-
to-cdr
![Page 32: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/32.jpg)
Example
count-leaves
![Page 33: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/33.jpg)
Recursive Stack
(defun factorial (n)
(if (= 0 n) 1 (* n (factorial (1- n)))
n = 5
n = 4
n = 3
n = 2
n = 1
n = 0 1
(* 1 1)
(* 2 1)
(* 3 2)
(* 4 6)
(* 5 24)
120
![Page 34: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/34.jpg)
Tail Recursion
Avoid revisiting previous context = no after-the-fact assembly
New formulation accumulate answer in parameter base case = return answer recursive step
• do partial solution and pass as answer parameter Effect
do need to revist context recursive call = GOTO!
![Page 35: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/35.jpg)
Tail Recursive Factorial
(defun factorial (n) (factorial-1 n 1))
(defun factorial-1 (n ans)
(if (= 0 n) ans (factorial-1 (1- n) (* n ans))))
n = 5, ans = 1
n = 4, ans = 5
n = 3, ans = 20
n = 2, ans = 60
n = 1, ans = 120
n = 0, ans = 120 120
120
120
120
120
120
120
![Page 36: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/36.jpg)
Exercise
average
![Page 37: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/37.jpg)
Symbolic Programming Example
iterators
![Page 38: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/38.jpg)
Homework #3
Crazy Eights Create a playable game
change the game state to use a closure
add a function that chooses a move for the computer players
![Page 39: Functions Robin Burke CSC 358/458 4.10.2006. Outline Errata Homework #2 Project Structures Functions review parameter lists anonymous fns closures recursion.](https://reader035.fdocuments.in/reader035/viewer/2022062620/5519c021550346695e8b46d5/html5/thumbnails/39.jpg)
Lab