cse 341, s. tanimoto lisp 3 - 1 defining functions with defun functions are the primary abstraction...

14
CSE 341, S. Tanimoto 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures and macros). Non-built-in Functions are usually defined using the special form DEFUN. > (defun sqr (x) (* x x)) SQR > (defun sqr (y) Returns the square of its arg." (* y y) ) SQR

Upload: gertrude-boyd

Post on 31-Dec-2015

248 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

1

Defining Functions with DEFUNFunctions are the primary abstraction mechanism available in Lisp. (Others are structures and macros).

Non-built-in Functions are usually defined using the special form DEFUN.

> (defun sqr (x) (* x x))SQR> (defun sqr (y) ”Returns the square of its arg." (* y y) )SQR

Page 2: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

2

Functions with no argumentsA function can take zero, one, or more arguments.

> (defun say-hello () "prints a greeting." (format t "Hello there!~%"))SAY-HELLO> (say-hello)Hello there!NIL>

Page 3: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

3

Symbol binding, function propertyA symbol can have a value and a function binding at the same time.

> (setq foo 10)10> (defun foo (n) "Returns n plus two." (+ n 2) )FOO> (describe 'foo)FOO is a SYMBOL. Its value is 10 It is INTERNAL in the COMMON-LISP-USER package. Its function binding is #<Interpreted Function FOO> The function takes arguments () Its property list has these indicator/value pairs:EXCL::%FUN-DOCUMENTATION "Returns n plus two.">

Page 4: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

4

COND (an alternative to IF)> (defun small-prime-p (n) "Returns T if n is a small prime." (cond ((= n 2) t) ((= n 3) t) ((= n 5) t) ((= n 7) t) (t nil) ) )SMALL-PRIME-P> (small-prime-p 9)NIL> (small-prime-p 3)T

Page 5: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

5

Recursive FunctionsA function may be defined in terms of itself.

> (defun factorial (n) "Returns factorial of N." (if (= n 1) 1 (* n (factorial (- n 1))) ) )FACTORIAL> (factorial 5)120> (factorial 20)2432902008176640000

Page 6: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

6

Primitive List-Manipulating Functions> (setq x '(a b c d))(A B C D)> (first x) ; FIRST returns 1st elt of listA> (rest x) ; REST returns all but 1st.(B C D)> x(A B C D) ; X has not been changed.> (cons 'a 'b)(A . B) ; CONS combines two things.> (cons 'a nil)(A) ; The result is often a list.> (cons 'z x)(Z A B C D)

Page 7: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

7

CAR, CDR, and their combinations> (setq x '(a b c d))(A B C D)> (car x) A ; CAR is equivalent to FIRST> (rest x) (B C D) ; CDR is equivalent to REST.> (cdr (cdr x))(C D)> (cddr x)(C D)> (caddr x)C ; CADDR is equivalent to THIRD

Page 8: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

8

Recursive Functions of Lists

> (defun censor (lst) "Returns LST with no instances of BAD." (cond ((null lst) nil) ((eq (car lst) 'BAD) (censor (rest lst)) ) (t (cons (car lst) (censor (rest lst)) ) ) ) )CENSOR> (censor '(This is a bad bad list))(THIS IS A LIST)

Page 9: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

9

One-Way Recursion Doesn’t Do Sublists

> (censor '(This bad list (has a bad sublist)))(THIS LIST (HAS A BAD SUBLIST))

Page 10: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

10

Two-Way Recursive Functions> (defun censor2 (lst) "Returns LST with no instances of BAD." (cond ((null lst) nil) ((atom lst) lst) ((eq (car lst) 'BAD) (censor2 (rest lst)) ) (t (cons (censor2 (car lst)) (censor2 (rest lst)) ) ) ) )CENSOR2> (censor2 '(This bad list (has a bad sublist)))(THIS LIST (HAS A SUBLIST))

Page 11: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

11

Looping: DOTIMES> (defun promise (n) "Prints a promise N times." (let ((the-promise "I will balance parentheses")) (dotimes (i n) (format t "~d. ~A.~%" i the-promise) ) ) )PROMISE> (promise 3)0. I will balance parentheses.1. I will balance parentheses.2. I will balance parentheses.NIL>

Page 12: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

12

Looping: DOLIST> (defun print-on-separate-lines (lst) "Prints LST with one line per element." (dolist (elt lst nil) (print elt) ) )PRINT-ON-SEPARATE-LINES> (print-on-separate-lines '(lunch around the corner) )

LUNCHAROUNDTHECORNERNIL>

Page 13: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

13

Pure FunctionsA pure function is one whose returned value depends only on its arguments (i.e., it possesses referential transparency), and it does not have any side effects.

(defun plus3 (x)(+ x 3)) ; a pure function

(defun plus3-with-y (x) ; not a pure function (setq y 3) (+ x y) )

(defun plus3-with-z (x) ; not pure unless (+ x z) ) ; z is constant

Page 14: CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures

CSE 341, S. Tanimoto Lisp 3 -

14

Functional Programming

Pure functional programming is programming using only pure functions.

No assignment statements, no loops.