Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Robin Burke CSC 358/458 4.10.2006.

Similar presentations


Presentation on theme: "Functions Robin Burke CSC 358/458 4.10.2006."— Presentation transcript:

1 Functions Robin Burke CSC 358/458

2 Outline Errata Homework #2 Project Structures Functions Example review
parameter lists anonymous fns closures recursion tail recursion Example

3 CLOS solution How many?

4 Errata append does cons it build a new list for the front part
shares structure with the last part

5 Homework #2

6 Project Develop your own Lisp application
Not restricted to implementing a game Timeline 4/17 Proposal 5/1 Accepted proposal 5/22 Progress report 6/7 Due

7 Structures Annoying to define a pile of accessors Alternative
defstruct Defines a structure a named type with slots

8 defstruct Defines accessors mutators constructor read macro

9 Example (defstruct card suit rank) Defines make-card card-suit
card-rank also #S(CARD :SUIT S :RANK 1) will be read and turned into a card object

10 Functions Defined with defun, but Real issue just a convenience
symbol-function binding

11 Calling Functions Normal evaluation Others (+ 1 2 3) funcall apply
avoid eval

12 Multiple values Let equivalent setq equivalent Example Also
multiple-value-bind setq equivalent multiple-value-setq Example (multiple-value-bind (div rem) (floor num 10) ... rest of code ...) Also multiple-value-list

13 Example add-digits

14 Parameter Lists keywords rest optional (defun foo (a &key b c) ... )
(foo 5 :b 10 :c 19) (foo 5) rest (defun foo (&rest lst) ... ) (foo) (foo ) optional (defun foo (a &optional b c) ... ) (foo )

15 Anonymous Functions Like anonymous classes in Java lambda
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

16 Example numeric series

17 Closures Shared lexical variable Closure
(defun integers () (let ((n 0)) (values #'(lambda () (incf n)) #'(lambda () (setf n 0))))) Closure binds variables in external scope keeps them when the scope disappears

18 Environment Evaluation takes place in an environment
bindings for symbols Hierarchy of environments global environment local environments "Lexical context"

19 Example (let ((var1 (baz arg1 arg2))) #'(lambda () (foo var1))))
(defun bar (arg1 arg2) (let ((var1 (baz arg1 arg2))) #'(lambda () (foo var1)))) (setf fn (bar 'a 'b))

20 When Closure is Created

21 When Closure is Returned

22 Encapsulation Achieves OO goal
variables in closure are totally inaccessible no way to refer to their bindings no way to even access that environment

23 Example Bank account

24 Compare with Java Anonymous classes External values only if
instance variables final variables

25 Anonymous Class (illegal)
JButton okButton = new JButton ("OK"); okButton.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent e) m_canceled = false; okButton.setEnabled(false); } });

26 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

27 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

28 Recursion Basic framework Why it works
solve simplest problem "base case" solve one piece combine with the solution to smaller problem Why it works eventually some call = base case then all partial solutions are combined

29 Recursive Statement Some problems have a natural recursive statement
maze following pattern matching

30 Example pattern matching

31 Double Recursion Trees Strategy
operating on the "current" element isn't simple recurse on both car and cdr Strategy base case combine solution-to-car with solution-to-cdr

32 Example count-leaves

33 Recursive Stack (if (= 0 n) 1 (* n (factorial (1- n)))
(defun factorial (n) (if (= 0 n) 1 (* n (factorial (1- n)))

34 Tail Recursion Avoid revisiting previous context New formulation
= 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!

35 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))))

36 Exercise average

37 Symbolic Programming Example
iterators

38 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

39 Lab


Download ppt "Functions Robin Burke CSC 358/458 4.10.2006."

Similar presentations


Ads by Google