- 76 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about 'Functional Programming' - starbuck

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

### Functional Programming

05 Functions

Functions - Global Functions

- fboundp
- Tells whether there is a function with a given symbol as its name
- > (fboundp ‘+)T
- > (symbol-function ‘+)#<SYSTEM-FUNCTION +>

- (setf (symbol-function ‘add2) #’(lambda (x) (+ x 2)))
- > (add2 1)3

Functions - Global Functions

- (setffunc_name)
- (defun primo (lst) (car lst))
- (defun (setf primo) (vallst) (setf (car lst) val))
- > (let ((x (list ‘a ‘b ‘c))) (setf (primo x) 480) x)(480 B C)
- The first parameter : new value
- The remaining parameters : the arguments of this setf function

Functions - Global Functions

- Documentation string
- If you make a string the first expression in the body of a function defined with defun, then this string will become the function’s documentation string
- (defunfoo (x) “Implements an enhanced paradigm of diversity.” x)
- > (documentation ‘foo ‘function)“Implements an enhanced paradigm of diversity.”

Functions - Local Functions

- Functions defined via defun or setf of symbol-function are global functions
- You can access global functions anywhere

- Local functions can be defined with labels, which is a kind of let for functions
- (labels ((add10 (x) (+ x 10)) (consa (x) (cons ‘a x))) (consa (add10 3)))(A . 13)

Functions - Local Functions

- > (labels ((len (lst) (if (null lst) 0 (+ (len (cdrlst)) 1)))) (len ‘(a b c)))3

Functions - Parameter Lists

- &rest
- Insert &rest before the last variable in the parameter list of a function
- This variable will be set to a list of all the remaining arguments
- (defun our-funcall (fn &rest args) (apply fn args))

Functions - Parameter Lists

- &optional
- All the arguments after it could be omitted
- (defunphilosoph (thing &optional property) (list thing ‘is property))
- > (philosoph ‘death)(DEATH IS NIL)
- The explicit default of the optional parameter
- (defunphilosoph (thing &optional (property ‘fun)) (list thing ‘is property))
- > (philosoph ‘death)(DEATH IS FUN)

Functions - Parameter Lists

- &key
- A keyword parameter is a more flexible kind of optional parameter
- These parameters will be identified not by their position, but by symbolic tags that precede them
- > (defunkeylist (a &key x y z) (list a x y z))KEYLIST
- > (keylist 1 :y 2)(1 NIL 2 NIL)
- > (keylist 1 :y 3 :x 2)(1 2 3 NIL)

Functions – Example: Utilities

- > (single? ‘(a))T → returns true when its argument is a list of one element
- > (append1 ‘(a b c) ‘d)(A B C D) → adds an element to the end of the list
- > (map-int #’identify 10)(0 1 2 3 4 5 6 7 8 9) → returns a list of the results of calling the function on the integers from 0 to n-1

Functions – Example: Utilities

- > (map-int #’(lambda (x) (random 100)) 10)(85 40 73 64 28 21 40 67 5 32)
- > (filter #’(lambda (x) (and (evenp x) (+ x 10))) ‘(1 2 3 4 5 6 7)(12 14 16) → returns all the non-nil values returned by the function as it is applied to the elements of the list
- > (most #’length ‘((a b) (a b c) (a)))(A B C) → returns the element of a list with3 the highest score, according to some scoring function

Functions – Example: Utilities

(defun single? (lst)

(and (consp 1st) (null (cdrlst))))

(defunappendl (1st obj)

(append 1st (list obj)))

(defun map-int (fn n)

(let ((acc nil))

(dotimes (i n)

(push (funcall fn i) acc))

(nreverseacc)))

(defun filter (fn lst)

(let ((acc nil))

(dolist (x lst)

(let ((val (funcall fn x)))

(if val (push val acc))))

(nreverse acc)))

Functions – Example: Utilities

(defun most (fn 1st)

(if (null 1st)

(values nil nil)

(let* ((wins (car lst))

(max (funcall fn wins)))

(dolist (obj (cdrlst))

(let ((score (funcall fn obj)))

(when (> score max)

(setf wins obj

max score))))

(values wins max))))

Functions – Closures

- (defun combiner (x) (typecase x (number #’+) (list #’append) (t #’list)))
- (defun combine (&rest args) (apply (combiner (car args))args))
- > (combine 2 3)5
- > (combine ‘(a b) ‘(c d))(A B C D)

Functions – Closures

- When a function refers to a variable defined outside it, it is called a free variable
- Closure
- A function that refers to a free variable
- (defun add-to-list (num lst) (mapcar #’(lambda (x) (+ x num))lst))
- num is free

- (defun make-adder (n) #’(lambda (x) (+ x n))) → returns a function
- n is free

Functions – Closures

- (setf add3 (make-adder 3))#<CLOSURE: LAMBDA (X) (+ X N)>
- > (funcall add3 2)5
- (setf add27 (make-adder 27)) #<CLOSURE: LAMBDA (X) (+ X N)>
- > (funcall add27 2)29

Functions – Closures

- Make several closures share variables
- (let ((counter 0)) (defun reset ( ) (setf counter 0)) (defun stamp ( ) (setf counter (+ counter 1))))
- > (list (stamp) (stamp) (reset) (stamp))(1 2 0 1)
- You can do the same thing which a global counter, but this way the counter is protected from unintended references

Functions – Example: Function Builders

(defun disjoin (fn &rest fns)

(if (null fns)

fn

(let ((disj (apply #’disjoin fns)))

#’(lambda (&rest args)

(or (apply fn args) (apply disjargs))))))

Functions – Example: Function Builders

(defun conjoin (fn &rest fns)

(if (null fns)

fn

(let ((conj (apply #’conjoin fns)))

#’(lambda (&rest args)

(and (apply fn args) (apply conj args))))))

(defun curry (fn &rest args)

#’(lambda (&rest args2)

(apply fn (append args args2))))

(defunrcurry (fn &rest args)

#’(lambda (&rest args2)

(apply fn (append args2 args))))

(defun always (x) #’(lambda (&rest args) x))

Functions – Example: Function Builders

- > (mapcar (disjoin #’integerp #’symbolp) ‘(a “a” 2 3))(T NIL T T)
- > (mapcar (conjoin #’integerp #’oddp) ‘(a “a” 2 3))(NIL NILNIL T)
- > (funcall (curry #’- 3) 2)1
- > (funcall (rcurry #’- 3) 2)-1

Functions – Recursion

- Recursion plays a greater role in Lisp than in most other languages
- Functional programming
- Recursive algorithms are less likely to involve side-effects

- Recursive data structure
- Lisp’s implicit use of pointers makes it easy to have recursively defined data structures
- The most common is the list: a list is either nil, or a cons whose cdr is a list

- Elegance
- Lisp programmers care a great deal about the beauty of their programs
- Recursive algorithms are often more elegant than their iterative counterparts

- Functional programming

Functions – Recursion

- To solve a problem using recursion, you have to
- Show how to solve the problem in the general case by breaking it down into a finite number of similar, but smaller, problems
- Show how to solve the smallest version of the problem - the base case - by some finite number of operations

- <Example1> Finding the length of a proper list
- In the general case, the length of a proper list is the length of its cdr plus 1
- The length of an empty list is 0

Functions – Recursion

- <Example２> member
- Something is a member of a list if it is the first element, or a member of the cdr
- Nothing is a member of the empty list

- <Example3> copy-tree
- The copy-tree of a cons is a cons made of the copy-tree of its car, and the copy-tree of its cdr
- The copy-tree of an atom is itself

Functions – Recursion

- The obvious recursive algorithm is not always the most efficient
- E.g. Fibonacci function1. Fib(0)=Fib(1)=12. Fib(n)=Fib(n-1)+Fib(n-2)
- (defun fib (n) (if (<= n 1) 1 (+ (fib (- n 1)) (fib (- n 2)))))
- The recursive version is appallingly inefficient
- The same computations are done over and over

Functions – Recursion

- (defun fib (n) (do ((i n (- i 1)) (f1 1 (+ f1 f2)) (f2 1 f1)) ((<= i 1) f1)))
- The iterative version is not as clear, but it is far more efficient
- However, this kind of thing happen very rarely in practice

Functions

- Homework (Due April 14)
- Use the lambda function and closures to write a function called our-complement that takes a predicate and returns the opposite predicate. For example:> (mapcar (our-complement #’oddp) ‘(1 2 3 4 5 6))(NIL T NIL T NIL T)

Download Presentation

Connecting to Server..