more language features functional programming academic year 2005-2006 alessandro cimatti...

80
More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti [email protected]

Upload: noah-shields

Post on 05-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

More Language FeaturesFunctional Programming

Academic Year 2005-2006

Alessandro Cimatti

[email protected]

Page 2: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Homework• Write a program to normalize expressions for lambda calculus• Suggested steps:

– Define the ADT for lambda terms• constructors• recognizers

– Define substitution primitive– Define recognizer for redex’s – Define beta-reduction rule– Define the normalizer:

• recursively traverse the term looking for redex, and reduce• careful with infinite looping!!

• Some remarks– homework due by december 20– try to use recursive definitions– homework is closely related to course project!!!

• Any questions?

Page 3: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Higher Order functions

Page 4: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

apply

• The apply function is used to manipulate function objects

• ( apply <function> <arg list> )

> (+ 2 3 4 5)14> (apply '+ '(2 3 4 5))14> (apply 'equal '(12 14))nil> (apply 'cons '(as (you like it)))(as you like it)

Page 5: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

apply• apply can be used in functions, when a function is passed as a parameter • Example

> (defun f-on-range (f n) (f-on-range-iter f 0 n nil)) f-on-range > (defun f-on-range-iter (f l h acc) (if (> l h) acc (let ((res (apply f (list l)))) (f-on-range-iter f (+ l 1) h (cons res acc))))) f-on-range-iter

> (f-on-range 'oddp 5) (T NIL T NIL T NIL) > (f-on-range 'evenp 5) (NIL T NIL T NIL T) > (f-on-range '1+ 5) (6 5 4 3 2 1) > (f-on-range '1- 5) (4 3 2 1 0 -1) > (f-on-range 'sqrt 5) (2.23606797749979 2.0 1.7320508075688772 1.4142135623730951 1.0 0.0) > (f-on-range 'exp 5) (148.41315910257663 54.598150033144236 20.08553692318767 7.38905609893065

2.718281828459045 1.0)

Page 6: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

mapcar• mapcar iterates over a list and applies the specified function• Examples

> (mapcar 'oddp '(1 2 3 4))(t nil t nil)> (mapcar 'evenp '(1 2 3 4))(nil t nil t)> (defun square (x) (* x x) )square> (mapcar 'square '(1 2 3 4 5))(1 4 9 16 25)> (mapcar 'cons '(1 2 3 4 5) '(1 2 3 4 5))((1 . 1) (2 . 2) (3 . 3) (4 . 4) (5 . 5))> (mapcar 'cons '(a b c) '(1 2 3))((a . 1) (b . 2) (c . 3))> (mapcar 'abs '(3 -4 2 -5 -6))(3 4 2 5 6) > (let* ((l '(1 2 3 4 5)) (sqlist (mapcar 'square l))) (mapcar 'cons l sqlist))((1 . 1) (2 . 4) (3 . 9) (4 . 16) (5 . 25))

Page 7: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

mapcar

> (setq words '((one eins) (two zwei) (three drei)) )((one eins) (two zwei) (three drei)> (mapcar 'car words)(one two three)> (mapcar 'cadr words)(eins zwei drei)> (mapcar 'cdr words)((eins) (zwei) (drei))

Page 8: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Lambda functions• Lambda notation lets us declare unnamed functions.• Can be useful for one-use function, that can be passed e.g. as a

parameter to another function.• Declaration of Lambda function:

– (lambda ( <parameter-list> ) <fn-BODY> )• Example: an unnamed square function

– (lambda (x) (* x x))• Lambda functions are used in place of the function name:

> ((lambda (x) (* x x)) 2) 4

• Usage within mapcar

> (mapcar (lambda (x) (cons x (* x x))) '(1 2 3 4 5))((1 . 1) (2 . 4) (3 . 9) (4 . 16) (5 . 25))

Page 9: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

maplist• maplist is like mapcar except that the function is applied to the

lists and successive cdr's of those lists rather than to successive elements of the lists.

• For example:

> (maplist (lambda (x) (cons 'foo x)) '(a b c d))((foo a b c d) (foo b c d) (foo c d) (foo d))

> (maplist (lambda (x) (if (member (car x) (cdr x)) 0 1)) '(a b a c d b c))(0 0 1 0 1 1 1)

Page 10: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

mapcar vs maplist

> words((one eins) (two zwei) (three drei)> (mapcar 'reverse words)((eins one) (zwei two) (drei three))> (maplist 'reverse words)(((three drei) (two zwei) (one eins)) ((three drei) (two zwei)) ((three drei)))> (maplist 'cdr words)(((two zwei) (three drei)) ((three drei)) nil)

Page 11: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Functions for built-in functions> (member 1 '(1 (1 . 2) (3 . 4)))(1 (1 . 2) (3 . 4))

> (member '(1 . 2) '((1 . 2) (3 . 4)))NIL> (member '(1 . 2) '((1 . 2) (3 . 4)) :test #'equal)((1 . 2) (3 . 4))

> (merge 'list '(1 3 4 6 7) '(2 5 8) '<)(1 2 3 4 5 6 7 8)

> (sort '(a b) 'string-lessp)(A B)

> (sort '(b a) 'string-lessp)(A B)

> (sort '(b a c d a a f d a) 'string-lessp)(A A A A B C D D F)

> (sort '(b a c d a a f d a) 'string-greaterp)(F D D C B A A A A)

Page 12: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Functions and Macros

Page 13: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Functions• defun: user defined function• (defun fn-name parm-list fn-body)

> (defun add (a b) (+ a b))ADD> (add 20 30)50> (defun square (x) (* x x))SQUARE> (square 5)25> (defun hypotenuse (x y) (sqrt (+ (square x) (square y)))HYPOTENUSE> (hypotenuse 3 4)5

• use let and let* forms to store partial computations (cfr countsum)– (defun add (a b)

(let ((ta a) (tb b)) (+ ta tb)))

Page 14: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Macros• Macros: the computer equivalent of abbreviations.• In LISP, the result is itself a bit of code to be executed• This is more self documenting (and more efficient in many

interpreters).• Example

– suppose we have some lists of the form (city state zip)– we can create a macro get-state-field that results in the code (car (cdr list))

• Defining Macros: defmacro– syntax similar to defun– When the macro is called, it is first evaluated for its result, then the

result is evaluated.

Page 15: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Macros

> (defmacro get-state-field (x) (list 'car (list 'cdr x)))get-state-field> (get-state-field '(GFK ND 58201))ND> (macroexpand '(get-state-field '(GFK ND 58201)) )(CAR (CDR (QUOTE (GFK ND 58201))))

> (defmacro simple-incf (var) (list 'setq var (list '+ var 1)))simple-incf> (setq a 4)4> (simple-incf a)5> (macroexpand '(simple-incf a))(SETQ A (+ A 1))

Page 16: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Backquote notation> (setq a 3)3> '(if a is true (and a a) then)(if a is true (and a a) then)> `(if ,a is true ,(and a a) then)(if 3 is true 3 then)> (list 'if a 'is 'true (list a a) 'is 'true)(if 3 is true (3 3) is true)> (list 'if a 'is 'true (and a a) 'is 'true)(if 3 is true 3 is true)> (setq b '(a b c))(a b c)> `(hello fred ,b)(hello fred (a b c))> `(hello fred ,@b)(hello fred a b c)

Page 17: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Macros> (defun name (x) (car x))name> (name '(a red table))a> (defmacro name (x) (list 'car x))name> (name '(a red table))a> (defmacro name (x) `(car ,x))name> (name '(a red table))a

Page 18: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Recursive Macros?> (defmacro f (n) `(if (< ,n 0) 1 (f (- ,n 1))))F> (f 1)

Stack overflow (stack size 16000). 1 (abort) Return to level 0. 2 Return to top loop level 0.

Type :b for backtrace, :c <option number> to proceed, or :? for other options

1 > :bCall to COMMON-LISP::REAL-STACK-OVERFLOW-AUXCall to RUNTIME:BAD-ARGS-OR-STACKCall to LEXICAL::ENSURE-ENVIRONMENTCall to MACRO-FUNCTIONCall to MACROEXPAND-1Call to LEXICAL:CAREFUL-MACROEXPANDCall to LEXICAL::PRECall to MAPCARCall to LEXICAL::LEX-CALLCall to LEXICAL::PRECall to MAPCARCall to LEXICAL::LEX-CALLCall to LEXICAL::PRE...

Page 19: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

&optional parameter

> (defmacro if2 (a b &optional c) `(cond (,a ,b) (t ,c) ) )if2

> (macroexpand '(if2 P (cons 1 2) (cons 2 3)))(IF P (CONS 1 2) (COND (T (CONS 2 3))))

> (macroexpand-1 '(if2 P (cons 1 2) (cons 2 3)))(COND (P (CONS 1 2)) (T (CONS 2 3)))

> (macroexpand-1 '(if2 P (cons 1 2)))(COND (P (CONS 1 2)) (T NIL))

> (setq P nil)nil> (if2 P (cons 1 2) (cons 2 3)))(2 . 3)> (if2 P (cons 1 2)))nil

Page 20: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

&rest

> (defun list2 (&rest x) x) list2> (list2 1 2 3 4 5)(1 2 3 4 5)

> (defun mm (fn &rest x) (mapcar fn x)) MM> (mm 'oddp 1 2 3 4 5 6)(T NIL T NIL T NIL)> (mm 'oddp)NIL

Page 21: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

&rest> (defmacro let*2 (x &rest forms) (if (null x) `(progn ,@forms) `(let (,(car x)) (let*2 ,(cdr x) ,@forms) ) ) )let*2

> (let*2 ((a 1) (b (+ a 1)) (c (cons a b))) (list a b c))(1 2 (1 . 2))

> (macroexpand '(let*2 ((a 1) (b (+ a 1)) (c (cons a b))) (list a b c)))(LET ((A 1)) (LET*2 ((B (+ A 1)) (C (CONS A B))) (LIST A B C)))

> (macroexpand '(LET*2 ((B (+ A 1)) (C (CONS A B))) (LIST A B C)))(LET ((B (+ A 1))) (LET*2 ((C (CONS A B))) (LIST A B C)))

> (macroexpand '(LET*2 ((C (CONS A B))) (LIST A B C)))(LET ((C (CONS A B))) (LET*2 NIL (LIST A B C)))

> (macroexpand '(LET*2 NIL (LIST A B C)))(PROGN (LIST A B C))

Page 22: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Macros: example

> (setq *clause-database* nil)nil> (defmacro defclause (head &rest body) `(let ((newclause (cons (quote ,head) (quote ,body)))) (setq *clause-database* (cons newclause *clause-database*))))defclause> (defclause P Q R S)((P Q R S))

> (macroexpand '(defclause P Q R S))(LET ((NEWCLAUSE (CONS (QUOTE P) (QUOTE (Q R S))))) (SETQ *CLAUSE-DATABASE* (CONS NEWCLAUSE *CLAUSE-DATABASE*)))

> (defclause P R (not S) (not U))((P R (NOT S) (NOT U)) (P Q R S))

> (macroexpand '(defclause P R (not S) (not U)))(LET ((NEWCLAUSE (CONS (QUOTE P) (QUOTE (R (NOT S) (NOT U)))))) (SETQ *CLAUSE-DATABASE* (CONS NEWCLAUSE *CLAUSE-DATABASE*)))

> *CLAUSE-DATABASE*((P R (NOT S) (NOT U)) (P Q R S))

Page 23: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

More Control Structures

or:

how “functional” can we be?

Page 24: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Command Sequences• progn returns the result of the last element, but evaluates all s-

expressions in the argument list

> (progn

(setq a 10)

(setq b 20)

(* 3 5))

15

> a

10

> b

20

Page 25: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

prog1• prog1 returns the result of the first element, but evaluates all s-

expressions in the argument list

> (progn (setq x 'foo) (setq x 'bar) (setq x 'baz) 'done)done> xbaz> (prog1 (setq x 'foo) (setq x 'bar) (setq x 'baz) 'done)foo> xbaz

Page 26: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

prog and loops> (defun expt (m n) (prog ((result 1) (expon n)) loop1 (if (zerop expon) (return result)) (setq result (* m result)) (setq expon (1- expon)) (go loop1) ))expt> (expt 2 5)32

Page 27: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Do you really like prog and loop?

> (defun expt (base n) (prog ((res 1) (expon n)) loop1 (if (zerop expon) (return res)) (setq res (* base res)) (setq expon (1- expon)) (go loop1)))

> (defun expt1 (m n) (expt1-iter m n 1))> (defun expt1-iter (base expon res) (if (zerop expon) res (expt1-iter base (1- expon) (* base res))))

Page 28: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Iteration: dotimes• dotimes iterates a specified numebr of times• Syntax

(dotimes (index-var upper-bound result-var) BODY)

> (dotimes (j 10 val) (setq val j))9> (setq val nil)NIL> (dotimes (j 10 val) (setq val (cons j val)))(9 8 7 6 5 4 3 2 1 0)> (dotimes (j 10 val) (setq val j))9> (defun add1 (x) (let ((val 0)) (dotimes (j 10 val) (setq val (+ j val)))))add1> (add1 10)45

Page 29: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

dotimes> (dotimes (x 12))NIL> (dotimes (x 12) nil)NIL> (dotimes (x 12 x) nil)12> (dotimes (x 12 (* x x)) nil)144

> (dotimes (x 3 x) (print (cons x (* x x))))0123> (dotimes (x 5) (print (cons x (* x x))) (print (list x)))

(0 . 0) (0) (1 . 1) (1) (2 . 4) (2) (3 . 9) (3) (4 . 16) (4) NIL

Page 30: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Usage of dotimes: palindromep;;; True if the specified subsequence of the string is a ;;; palindrome (reads the same forwards and backwards).

(defun palindromep (string &optional (start 0) (end (length string))) (dotimes (k (floor (- end start) 2) t) (unless (char-equal (char string (+ start k)) (char string (- end k 1))) (return nil))))

> (palindromep "abcba")T> (palindromep "XXXabcba")NIL> (palindromep "XXXabcba" 3)T> (palindromep "XXXabcbaYYY" 3)NIL> (palindromep "XXXabcbaYYY" 3 8)T

> (palindromep "Able was I ere I saw Elba")T > (palindromep "A man, a plan, a canal-Panama!")nil > (remove-if-not 'alpha-char-p ;Remove punctuation "A man, a plan, a canal-Panama!")"AmanaplanacanalPanama" > (palindromep (remove-if-not #'alpha-char-p "A man, a plan, a canal-Panama!"))T

Page 31: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Iteration: dolist• dolist has the same syntax as dotimes, but instead of counting, it

steps the index variable through the elements of a list.(dolist (next-element target-list result) BODY)> (defun num-sublists (lis) (let ((result 0)) (dolist (next lis result)

(if (listp next) (setq result (1+ result))))))num-sublist> (num-sublists '(a b (c (d)) (e f) g (h i)))3> (num-sublists '(a b))0> (num-sublists '())0> (num-sublists '0)Error: 0 is not of type LIST.

Page 32: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

dolist

> (dolist (x '(a b c) y) (setq y (list x)) )(c)> (let ((y nil)) (dolist (x '(a b c) y) (setq y (cons x y)) ))(C B A)

Page 33: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Iteration: do(do ((var1 init1 [update1])

(var2 init2 [update2])...) (test action-1 ... action-n)

BODY)

> (defun length1 (x)

(do ((lst x (cdr lst))

(count 0 (+ 1 count)))

((null lst) count)))

length1

> (length1 ’(a b c (d e)))

4

Page 34: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

do

> (defun count (L) (do ((cnt 0 (+ cnt 1)) (loaf L (cdr loaf))) ((null loaf) cnt) ) )count> (count '(a (b c) d e))4

Page 35: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

do

> (defun fact (x) (do ((n 1 (1+ n)) (res 1) ) ((> n x) res) (setq res (* res n)) ) )fact> (fact 6)720

Page 36: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

List Surgery

or:

how far from functional can we be?

Page 37: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Memory Model

• Memory management is hidden from the user• Whenever a cons cell is needed, it is created fresh

– unless otherwise specified!• Free nodes list is a list of the nodes that are available

– big chunks of memory are requested to the OS, and organized in the free node list

– this is to avoid expensive OS calls• Garbage collection sweeps the list of memory cells

and collects information about their usage– the ones that are not used (i.e. have no incoming pointers)

are reinserted in the free nodes list• Once the available free list is exhausted, more

memory is requested to the OS

Page 38: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Equal and Eq> (setq L1 (list 1 2 3))> (setq L2 (list 1 2 3))> (setq L3 L2)

> (equal L1 L2)T> (equal L2 L3)T

> (eq L1 L2)nil> (eq L2 L3)T

> (setq L3 (cons (first L3) (rest L3)))> (eq L2 L3)nil> (eq (rest L2) (rest L3))t

Page 39: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

List Surgery

> (setq a '(a b c))(a b c)> (setq b '(d e f))(d e f)> (append a b)(a b c d e f)> (nconc a b)(a b c d e f)> a(a b c d e f)> b(d e f)

Page 40: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

List Surgery

> (setq abc '(a b c))

(a b c)

> (setq xyz '(x y z))

(x y z)

> (setq bc (cdr abc))

(b c)

> (setq yz (cdr xyz))

(y z)

> (setq a (nconc abc xyz))

(a b c x y z)

> a

(a b c x y z)

> abc

(a b c x y z)

> xyz

(x y z)

> bc

(b c x y z)

> yz

(y z)

Page 41: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

More on nconc> (last a)(3)> (last '(a b (c)))((C))> (setq a '(1 2 3))(1 2 3)> (setq b '(4 5 6 ))(4 5 6)> (nconc a 1)(1 2 3 . 1)> (nconc a nil)(1 2 3)> a(1 2 3)> (nconc a b)(1 2 3 4 5 6)> a(1 2 3 4 5 6)> b(4 5 6)> (nconc b 3)(4 5 6 . 3)> b(4 5 6 . 3)> a(1 2 3 4 5 6 . 3)

> (nconc a b)(1 2 3 4 5 6)> a(1 2 3 4 5 6)> b(4 5 6)> (nconc b 3)(4 5 6 . 3)> b(4 5 6 . 3)> a(1 2 3 4 5 6 . 3)> (setq *print-circle* t)T> (nconc b a)#1=(4 5 6 1 2 3 . #1#)> b#1=(4 5 6 1 2 3 . #1#)> (eq b (rest (rest (rest a))))T> (equal b (rest (rest (rest a))))T> (eq a (cdr (cdr (cdr (cdr (cdr (cdr a))))))) T

Page 42: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Rplaca and Rplacd> (setq a '(a b c d e))(a b c d e)> (rplaca a '(a b))((a b) b c d e)> a((a b) b c d e)> (rplacd a '(x y z))((a b) x y z)

> (setq a (cons 7 9))(7 . 9)

> (rplaca a a)(((((((((((((((((((((((((((( ...

(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((

Stack overflow (stack size 16000). > (setq *print-level* 5)5

> a(((((# . 9) . 9) . 9) . 9) . 9)

> (setq *print-circle* t)T

> a#1=(#1# . 9)

> (rplacd a a)#1=(#1# . #1#)

Page 43: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

List Surgery> (setq a '(a b c d e f))(a b c d e f)> (delete 'a a)(b c d e f)> a(a b c d e f)> (delete 'e a)(a b c d f)> a(a b c d f)> (setq a (delete 'a a))(b c d f)> a(b c d f)> (delete 'a '(a b a c a d a e))(b c d e)

Page 44: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Basic Assignments

• set: binds a s-expression to a symbol. i.e. assignment of value to a symbol which is

evaluated from the 1st parameter.– (set ’a 10) 10– a 10

• setq: assignment of value to a symbol– (setq x 10) 10– x 10

• setf: gets the memory location of the 1st parameter and sets its value.– (setf x 10)

Page 45: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Property Lists

Page 46: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Setf on lists

• Replacement of list element with setf:> (setf x ’(a (b c) d)

(a (b c) d)

> (cadadr x)

c

> (setf (cadadr x) ’123)

123

> x

(a (b 123) d)

Page 47: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Associative lists

• Associative list– implements a function from a finite set of keys– to a finite set of values

>(setq sarah '((height 6) (weight 100) (sex "F")))((HEIGHT 6) (WEIGHT 100) (SEX "F"))>(assoc 'weights sarah)(WEIGHT 100)

• A symbol is associated with a built-in structure called property list– an associative list of properties

Page 48: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Property Lists> (symbol-plist 'fred)nil

> (setf (symbol-plist 'fred) '(sex male age 23 sibs (bob carol)))(sex male age 23 sibs (bob carol))> (symbol-plist 'fred)(SEX MALE AGE 23 SIBS (BOB CAROL))

> (get 'fred 'age)23> (get 'fred 'sex)MALE> (get 'fred 'sibs)(BOB CAROL)> (get 'fred 'height)nil

> (setf (get 'fred 'age) 25)25> (setf (get 'fred 'sex) 'female)FEMALE> (setf (get 'fred 'sibs) '(BEN JERRY))(BEN JERRY)> (symbol-plist 'fred)(SEX FEMALE AGE 25 SIBS (BEN JERRY))

> (remprop 'fred 'sex)(SEX MALE AGE 23 SIBS (BEN JERRY))> (symbol-plist 'fred)(AGE 23 SIBS (BEN JERRY))> (remprop 'fred 'age)(AGE 23 SIBS (BEN JERRY))> (symbol-plist 'fred)(SIBS (BEN JERRY))

Page 49: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Property Lists

• Property lists in Lisp allow us to use a symbol to represent a structured object (e.g. a record)

• set, get• (setf (get ’chair ’color) ’blue)• (get ’chair ’color) blue• The interpreter does it by building a list of the form (property

value property value)• It’s recommended to use something other than nil for a false

value in property lists because getting a non-assigned value also returns nil.

Page 50: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Property Lists

• Alternately, get can be assigned a 3rd parameter to return if it doesn’t find the property defined in the list

• (get ’chair ’clour ’unknown) : returns unknown for the typo.

• Property list changes made to local symbols are not necessarily local (cfr surgery)

• Property lists need not be bound to a symbol• you can define your own property lists as lists of the form

(property value property value …)– Again: careful, setf will modify the actual list in places

where it would otherwise be copied then.

Page 51: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Arrays

Page 52: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Basics: Arrays• make-array: create an array• aref: array reference• setf: set value of array element• Index from 0 to limit - 1• Example:

> (setq a (make-array ’(3 3)))#2A((NIL NIL NIL) (NIL NIL NIL) (NIL NIL NIL))> (aref a 2 2)NIL> (setf (aref a 2 2) 100)100> a#2A((NIL NIL NIL) (NIL NIL NIL) (NIL NIL 100))> (setq (aref a 2 2) 100)Error: *** - SETQ: (AREF A 2 2) is not a symbol

Page 53: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

More on Arrays(make-array '(4 2 3) :initial-contents '(((a b c) (1 2 3)) ((d e f) (3 1 2)) ((g h i) (2 3 1)) ((j k l) (0 0 0))))

(vector &rest objects) is a convenient way to create monodimensional arrays

(vector 1 2 3 4 5) stands for (make-array (list 5) :element-type t :initial-contents (list 1 2 3 4 5))

Page 54: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Characters and Strings

Page 55: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Characters• Characters are printed as preceded by #\

• From numerical code to character with code-char

• Example– (dotimes (i 256) (print (code-char i)))

#\Null #\SOH #\STX #\ETX #\EOT #\ENQ #\ACK #\Bell #\Backspace #\Tab #\Newline #\VT #\Page #\Return #\SO #\SI #\DLE #\DC1 #\DC2 #\DC3 #\DC4 #\NAK #\SYN #\ETB #\CAN #\EM #\SUB #\Escape #\FS #\GS #\RS #\US #\Space #\! #\" #\# #\$ #\% #\& #\' #\( #\) #\* #\+ #\, #\- #\. #\/ #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\: #\; #\< #\= #\> #\? #\@ #\A #\B #\C #\D #\E #\F #\G #\H #\I #\J #\K #\L #\M #\N #\O #\P #\Q #\R #\S #\T #\U #\V #\W #\X #\Y #\Z #\[ #\\ #\] #\^ #\_ #\` #\a #\b #\c #\d #\e #\f #\g #\h #\i #\j #\k #\l #\m #\n #\o #\p #\q #\r #\s #\t #\u #\v #\w #\x #\y #\z #\{ #\| #\} #\~ #\Rubout #\U+0080 #\U+0081 #\U+0082 #\U+0083 #\U+0084 #\U+0085 #\U+0086 #\U+0087 #\U+0088 #\U+0089 #\U+008A #\U+008B #\U+008C #\U+008D #\U+008E #\U+008F #\U+0090 #\U+0091 #\U+0092 #\U+0093 #\U+0094 #\U+0095 #\U+0096 #\U+0097 #\U+0098 #\U+0099 #\U+009A #\U+009B #\U+009C #\U+009D #\U+009E #\U+009F #\No-reak-pace #\¡ #\¢ #\£ #\¤ #\¥ #\¦ #\§ #\¨ #\© #\ª #\« #\¬ #\Soft-Hyphen #\® #\¯ #\° #\± #\² #\³ #\´ #\µ #\¶ #\· #\¸ #\¹ #\º #\» #\¼ #\½ #\¾ #\¿ #\À #\Á #\Â #\Ã #\Ä #\Å #\Æ #\Ç #\È #\É #\Ê #\Ë #\Ì #\Í #\Î #\Ï #\Ð #\Ñ #\Ò #\Ó #\Ô #\Õ #\Ö #\× #\Ø #\Ù #\Ú #\Û #\Ü #\Ý #\Þ #\ß #\à #\á #\â #\ã #\ä #\å #\æ #\ç #\è #\é #\ê #\ë #\ì #\í #\î #\ï #\ð #\ñ #\ò #\ó #\ô #\õ #\ö #\÷ #\ø #\ù #\ú #\û #\ü #\ý #\þ #\ÿ

NIL

Page 56: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Strings• Strings are specialized vectors whose elements are characters

• String specific operations can also accept symbols, provided that the string is not modified

(let ((res nil) (s "Could you show me the characters in this

string, please?")) (dotimes (i (length s) res) (setq res (cons (char s i) res))))(#\? #\e #\s #\a #\e #\l #\p #\Space #\, #\g #\n #\i

#\r #\t #\s #\Space #\s #\i #\h #\t #\Space #\n #\i #\Space #\s #\r #\e #\t #\c #\a #\r #\a #\h #\c #\Space #\e #\h #\t #\Space #\e #\m #\Space #\w #\o #\h #\s #\Space #\u #\o #\y #\Space #\d #\l #\u #\o #\C)

Page 57: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Strings(char s j) == (aref (the string s) j)

setf may be used with char to destructively replace a character within a string.

> (setq a "case")"case"

> (setf (aref a 2) #\n)#\n

> a"cane"

Page 58: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

String Comparison

string= string1 string2 &key :start1 :end1 :start2 :end2

string= compares two strings and is true if they are the same

string= is necessarily false if the (sub)strings being compared are of unequal length

(string= "foo" "bar") is false (string= "foo" "foo") is true (string= "foo" "Foo") is false (string= "together" "frog“ :start1 1 :end1 3 :start2 2) is true

Page 59: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

String Comparisonstring-equal is similar but ignores cases(string-equal "foo" "Foo") is false

string< string1 string2 &key :start1 :end1 :start2 :end2 string> string1 string2 &key :start1 :end1 :start2 :end2 string<= string1 string2 &key :start1 :end1 :start2 :end2 string>= string1 string2 &key :start1 :end1 :start2 :end2 string/= string1 string2 &key :start1 :end1 :start2 :end2

string-lessp string1 string2 &key :start1 :end1 :start2 :end2string-greaterp string1 string2 &key :start1 :end1 :start2 :end2 string-not-greaterp string1 string2 &key :start1 :end1 :start2 :end2 string-not-lessp string1 string2 &key :start1 :end1 :start2 :end2 string-not-equal string1 string2 &key :start1 :end1 :start2 :end2

Page 60: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

New symbols

> (gentemp "VAR")VAR4

> (gentemp "VAR")VAR5

> (gentemp "VAR")VAR6

> (gentemp "VAR")VAR7

> (list '(VAR6 VAR7 VAR8))((VAR6 VAR7 VAR8))

> (gentemp "VAR")VAR9

Page 61: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Merging strings

> (concatenate 'string

"be " "my " "guest!")

"be my guest!"

> (concatenate 'list

"be " "my " "guest!")(#\b #\e #\Space #\m #\y #\Space #\g #\u #\e #\s #\t #\!)

Page 62: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Example: String to integer(defun convert-string-to-integer (str &optional (radix 10)) "Given a digit string and optional radix, return an

integer." (do ((j 0 (+ j 1)) (n 0 (+ (* n radix) (or (digit-char-p (char str j) radix) (error "Bad radix-~D digit: ~C" radix (char str j)))))) ((= j (length str)) n)))

Page 63: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Printing and Reading

Page 64: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Printing> (progn (prin1 "a asdj")(prin1 "a asdj")(prin1 "a asdj") (values))"a asdj""a asdj""a asdj"

> (progn (princ "a asdj")(princ "a asdj")(princ "a asdj") (values))a asdja asdja asdj

> (progn (princ "a asdj") (terpri) (princ "a asdj") (terpri) (princ "a asdj") (values))a asdja asdja asdj

> (progn (print "a asdj")(print "a asdj")(print "a asdj") (values))

"a asdj" "a asdj" "a asdj"

Page 65: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Output• format: format a string

(format dest string args) dest: determines what to return

t: return NILNIL: return string

– The format function normally returns NIL, but as a side effect it causes things to be written on the display or to a file.

– 1st argument: the symbol T when we want to write to the display.– 2nd argument: the format control string.

• ~%: move to a new line• ~&: move to a new line unless it knows it’s already at the beginning of a

new line.• ~S: inserts the printed representation of a Lisp object into the message

that Format prints.> (format t ”Hi, mom! ~% This is ~S” ’Tom)

Hi, mom! This is Tom

Nil

Page 66: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

format> (format t "the symbol ~s appeared ~s times ~%" 'a 3)

the symbol a appeared 3 times

nil

> (format t "~% Hello ~a old boy" "bruce")

 

Hello bruce old boy

nil

> (format t "~% Hello ~a old boy" "Bruce")

Hello Bruce old boy

NIL

> (format t "~% Hello ~s old boy" "Bruce")

Hello "Bruce" old boy

> (format t "~% Hello ~a old boy" '(a b c))

 

Hello (a b c) old boy

nil

Page 67: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Examples of READCL-USER 1 > (setq a (read))nilNIL

CL-USER 2 > aNIL

CL-USER 3 > (setq a (read))11

CL-USER 4 > (setq a (read))(1 . 2)(1 . 2)

CL-USER 5 > a(1 . 2)

CL-USER 6 > (setq a (read))(cons 1 2)(CONS 1 2)

CL-USER 7 > a(CONS 1 2)

CL-USER 8 > (eval a)(1 . 2)

CL-USER 9 > (setq a (read))kajsndkajsd KAJSNDKAJSD

CL-USER 10 > aKAJSNDKAJSD

CL-USER 13 > (setq a (read))(1.2)(1 . 2)

CL-USER 14 > a(1 . 2)

Page 68: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

input

• “read” is equivalent to “cin” in C++• Reads an entire expression from the keyboard

when called

• Syntax: read &optional input-stream eof-error-p eof-value

Page 69: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Streams

• Streams are objects that serve as sources or sinks of data. – character streams produce or absorb characters;– binary streams produce or absorb integers.

• The normal action of a Common Lisp system is to– read characters from a character input stream– parse the characters as representations of Common Lisp data objects, – evaluate each object (as a form) as it is read– and print representations of the results of evaluation to an output character stream.

• Typically streams are connected to files or to an interactive terminal. • Streams encapsulate external devices by which input/output is accomplished. • A stream, whether a character stream or a binary stream, may be

– input-only– output-only– bidirectional

• What operations may be performed on a stream depends on which of the six types of stream it is.

Page 70: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Standard Streams

• *standard-input*

• *standard-output*

• *error-output*

• *query-io*

• *debug-io*

• *terminal-io*

• *trace-output*

Page 71: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Creating new Streams• make-string-input-stream string &optional start end

• make-string-output-stream

• get-output-stream-string string-output-stream

• with-input-from-string (var string keyword {value}*)• {declaration}* {form}*

> (with-input-from-string (s "Animal Crackers" :index j :start 6) (read s)) crackers

> (with-input-from-string (s "This is Programming Languages" :index j) (list (read s) (read s) (read s) (read s) ))(THIS IS PROGRAMMING LANGUAGES)> j29> (with-input-from-string (s "This is PL") (list (read s) (read s) (read s) (read s) ))Error: EOF while reading stream < ... >

Page 72: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Streams from files

(with-open-file (<stream-name> <file-name> :direction :input or :output) ... )

internal variable name external file name

Page 73: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Streams

With-Open-File: to open files for output by passing it the special keyword argument :DIRECTION :OUTPUT.

The stream that With-Open-File creates can then be used in place of the usual T as a 1st argument to FORMAT.

(defun save-tree-data (tree-loc tree-table num-trees)(with-open-file (stream “/usr/dst/timber.outdat” :direction :output)

(format stream “~S~%” tree-loc)(format stream “~S~%” tree-table)(format stream “~S~%” num-trees)))

> (save-tree-data “The West Ridge” ‘((45 redwood) (22 oak) (43 maple))110)

NIL

Page 74: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

with-open-file; Create an external file "out.dat“ that contains the ; tabled form of the square function from 0 to 99(with-open-file (result "out.dat" :direction :output :if-does-not-exist :create :if-exists :overwrite) (dotimes (count 100) (print (cons count (* count count)) result)))NIL

; read sexpression from file and print it until end of file(with-open-file (stream "out.dat" :direction :input) (do ((elem (read stream nil :my_eof) (read stream

nil :my_eof))) ((equal elem :my_eof) elem) (print elem)))

Page 75: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

File Input

> (setq fp (open "plt.lsp" :direction :input))#<Character-Input-Stream 13:"a:\plt.lsp">> (setq a (read fp nil))(defun space (n) (cond ((< n 0) (princ "ERROR!")) ((zerop n) nil) (t (princ " ") (space (1- n)))))

> (close fp)t

Page 76: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Displaying File Contents

(do* ((fp (open "plot.lsp" :direction :input))

(ex (read fp nil) (read fp nil))

)

((null ex) nil)

(print ex)

)

Page 77: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

File copy

> (defun copy (fn1 fn2) (do* ((old (open fn1 :direction :input)) (new (open fn2 :direction :output)) (ex (read old nil) (read old nil)) ) ((null ex) (close old) (close new)) (print ex new) ))copy> (copy "plot.lsp" "temp.txt")t

Page 78: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

String Copy (let ((ss "kjasd kajsd")) (with-input-from-string (s ss) (dotimes (v (length ss)) (print (read s nil " ")))))

KJASD KAJSD " " " " " " " " " " " " " " " " " " " " NIL

Page 79: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Printing the characters of a string

> (let ((ss "kj \" | |, , , , ; ; ' ` asd , kajsd")) (with-input-from-string (s ss) (dotimes (v (length ss)) (print (read-char s)))))

#\k #\j #\Space #\" #\Space #\| #\Space #\| #\, #\Space #\, #\Space #\, #\Space #\, #\Space #\; #\Space #\; #\Space #\' #\Space #\` #\Space #\a #\s #\d #\Space #\, #\Space #\k #\a #\j #\s #\d

NIL

Page 80: More Language Features Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Load

• (compile-file “file_name”)– Compiles the filename– Compiled file has to be explicitly loaded

• (load “file_name”)– Loads and runs the filename.