More About Lisp 1 Functions on Lists setf
More About Lisp 1
Functions on Lists (setf x '(a b c)) => (A B C) (setf y '(1 2 3)) => (1 2 3) (rest x) => (B C) (nth 1 x) => B (car x) => A (cdr y) => (2 3) (reverse x) => (C B A) (cons 0 y) => (0 1 2 3) (append x y) => (A B C 1 2 3) (list x y) => ((A B C) (1 2 3)) (list* x y) =>((A B C) 1 2 3) (null nil) => T (null x) => NIL (listp x) => T (listp nil) => T (listp 3) => NIL (consp x) => T (consp nil) => NIL (equal x y) => NIL (equal x '(a b c)) => T (sort y #'>) => (3 2 1) (subseq x 1 2) => (B) ; ; all but the first element ; ; nth element of a list ; ; the first element of a list ; ; all but the first name ; ; puts list in reverse order ; ; add to front of list ; ; append together elements ; ; make a new list ; ; append last argument to others ; ; it is true for the empty list ; ; it is false for everything else ; ; it is true for any list ; ; including nil list ; ; it is false for non-list ; ; it is true for non-nil list ; ; it is false for atoms, including nil ; ; false for lists that looks different ; ; true for lists that look the same ; ; sort the list according to > function 2 ; ; subsequence with given start and end
Different equality functions eq – tests for the exact same object strict eql – tests for objects that are equivalent numbers equal – tests for objects that are lists or strings with eql elements equalp – likes equal except it matches upper- and lowercase characters and numbers of different types x ‘x ‘ 0 ‘(x) ‘”xy” ‘”Xy” ‘ 0 y ‘x ‘ 0 ‘(x) ‘”xy” ‘”x. Y” ‘ 0. 0 eq eql equal T T nil nil T T nil equalp T T T Others: = tree-equal char-equal string-equal 3
Functions on Tables ; ; table representation (setf capital. City-table ‘((UK. London) (JP. Tokyo) (CN. Beijing) (UAS. Washington DC))) ((UK. LONDON) (JP. TOKYO) (CN. BEIJING) (UAS. WASHINGTONDC)) (assoc 'JP capital. City-table) => (JP. TOKYO) (cdr (assoc 'JP capital. City-table)) => TOKYO (assoc 'FR capital. City-table) => NIL (rassoc 'Tokyo capital. City-table) => (JP. TOKYO) ; ; hash table representation (setf hash. Table (make-hash-table)) => #<HASHTABLE 0/211> (setf (gethash 'UK hash. Table) 'London) => LONDON (setf (gethash 'JP hash. Table) 'Tokyo) => TOKYO (setf (gethash 'CN hash. Table) 'Bei. Jing) => BEIJING (setf (gethash 'USA hash. Table) 'Washington. DC) => WASHINGTONDC (gethash 'UK hash. Table) => LONDON T (gethash 'FR hash. Table) => NIL ; ; property list (property/value) representation (setf (get 'UK 'capital) 'London) => LONDON (setf (get 'JP 'capital) 'Tokyo) => TOKYO (setf (get 'CN 'capital) 'Bei. Jing) => BEIJING (setf (get 'USA 'capital) 'Washington. DC) => WASHINGTONDC (get 'UK 'capital) => LONDON 4 (get 'FR 'capital) => NIL
Functions on Trees (setf a. Tree '((a b) ((c)) (d e))) ((A B) ((C)) (D E)) (tree-equal a. Tree '((a b) ((c)) (d e))) => T (tree-equal a. Tree '((1 2) ((3)) (4 5 ))) => NIL (defun same-shape-tree (a b) "Are two trees the same except for the leaves? " (tree-equal a b : test #'true)) SAME-SHAPE-TREE (defun true (&rest ignore) t) TRUE (same-shape-tree a. Tree '((1 2) ((3)) (4 5 ))) => T (same-shape-tree a. Tree '((1 2) ((3)) (4 5 6))) => NIL (subst '1 ‘c a. Tree) ((A B) ((1)) (D E)) (sublis '((a. 1)) a. Tree) ((A B) ((1)) (D E)) (defun english->french (words) (sublis '((are. va) (friend. ami) (hello. bonjour) (how. comment) (my. mon) (you. tu) ) words)) ENGLISH->FRENCH (english->french '(hello my friend - how are you today? )) (BONJOUR MON AMI - COMMENT VA TU TODAY? ) 5
Functions on Numbers (+ 4 2) => 6 (- 4 2) => 2 (* 4 2) => 8 (/ 4 2) => 2 (> 100 99) => T (= 100 101) => NIL (< 99 100) => T (random 100) => 68 (expt 4 2) => 16 (sin pi) => 0. 0 (asin 0) => 0. 0 (min 2 3 4) => 2 (max 4 5 2) => 5 (abs -3) => 3 (sqrt 4) => 2. 0 (round 4. 1) => 4 (rem 11 5) => 1 Functions on Sets (setf r '(a b c d)) => (A B C D) (setf s '(c d e)) => (C D E) (intersection r s) =>(C D) (union r s) => (A B C D E) (set-difference r s) => (A B) (member 'd r) => (D) (subsetp s r) => NIL (adjoin 'b s) => (B C D E) (adjoin 'c s) => (C D E) (bit-and #*11110 #*11001) => #*11000 (logand #b 11110 #b 11001) => 24 Others: logior, bit-ior logandc 2, bit-andc 2 logbitp, bit logcount 6
Data Types Type character number float integer fixnum bignum function symbol null keyword sequence list vector cons atom string array structure hash-table Example # c 42 3. 14 24 123456789 #’sin nil : key (a b c) #(a b c) t “a b c” #1 A(a b c) #S(type …) Explanation An integer that fits in a single word of storage An integer of unbounded size A function can be applied to an argument list Symbols can name fns and vars, and are themselves objects Keywords are a subtype of symbol Including lists and vectors A cons or null a subtype of sequence a non-nil list anything that is not cons (setf ar #1 A(a b c)) => #(A B C) (aref ar 1) => B (setf ar #2 A((a b c) (d f g))) #2 A((A B C)(D F G)) (aref ar 1 2) => G (defstruct name first vectors and higher-dimension arrays Structures are defined by defstruct It is created by make-hash-table last) NAME (setf st #S(name : first rose : last huang)) 7 #S( NAME : FIRST ROSE : LAST HUANG )
Data Types Type Example t nil complex bit rational ratio simple-array readtable package pathname stream random-state 52 Explanation Every object is of type t No object is of type nil #C(0 1) Imaginary numbers 0 0 or 1 2/3 Including integers and ratios 2/3 Exact fractional numbers #1 A(x y) An array that is not displaced or adjustable A mapping from characters to their meanings to read A collection of symbols that form a module #P”/usr/spool/mail” A file or directory name A pointer to an open file A state used as a seed by random 8
Input/Output prints any object on a new line. prin 1 prints any object without the new line and space. princ is used to print in a human-readable format. read for print and prin 1, the object is printed in a form that could be processed by read. for princ, a string is interpreted as symbols rather a string by read. write accepts eleven different keyword arguments that controls whether it acts like prin 1 or princ. (with-open-file (stream "test. text" : direction : output) (print '(hello there) stream) If you insert a line here (terpri stream) GOODBYE What is result in test. text file? (with-open-file (stream "test. text": direction : input) (princ 'goodbye stream)) In file, test. text: (HELLO THERE) GOODBYE (list (read stream) (read-char stream) (read stream nil 'eof))) ((HELLO THERE) G O ODBYE EOF) 9
Format terpri stands for “terminate print line” and it skips to the next line. fresh-line also skips to the next line, unless it can be determined that the output is already at the start of a line. format provides a very general function for doing formatted output. t is the first argument of format function. There are 26 different format directives ~ & moves to a fresh line ~ a prints the next argument as princ would. ~ s prints the next argument as prinl would. ~ f prints a number in float. ~ r prints the next argument, which should be a number, in English. ~ @r prints a number as a roman numeral. ~ {…~} takes the next argument, which must be a list, and formats each element of the list according to the format string inside the braces. ~ ^ exits from the enclosing ~ {…~} loop if there are no more arguments. 10
Examples of format: (format t "Hello, how are you? ") two plus "two" is 4. 0 NIL (format t "~&~a plus ~s is ~f" "two" 4) hello! how are you? NIL (let ((numbers '(1 2 3 4 5))) (format t "~&~{~r~^ plus ~} is ~f" numbers (apply #'+ numbers))) one plus two plus three plus four plus five is 15. 0 NIL 11
e. g. 1: temperature converter (defun f-to-c () (format t "~%Please enter Fahrenheit temperature: ") (let* ((ftemp (read)) (ctemp (* (- (first ftemp) 32) 5/9))) (format t "~%~s degrees Fahrenheit is ~s degrees Celsius~%" ftemp (float ctemp)) ; ; print floated value ; ; return ratio value F-TO-C (f-to-c) Please enter Fahrenheit temperature: (100) degrees Fahrenheit is 37. 77778 degrees Celsius 340/9 12
e. g. 2: math quiz (defun math-quiz (op range n) "ask the user a series of math questions. " (dotimes (i n) (question (random range) op (random range)))) MATH-QUIZ (defun question(x op y) "ask a math question, read a reply, and say if it is correct. " (format t "~&How much is ~d ~a ~d? " x op y) (if (eql (read) (funcall op x y)) (princ "correct!") (princ "sorry, that is not right. "))) QUESTION (math-quiz '+ 100 3) How much is 69 + 44? (113) correct! How much is 35 + 72? (107) correct! How much is 38 + 16? (40) sorry, that is not right. NIL 13
Exercise Problems If you have time, try to do the following exercise problems. 1. Write a function that can save (into a file) and print nine-nine table (九九表) in the table format. 2. Modify the example 2 (e. g. 2: math-quiz) in lecture note so that users does not need to remember three arguments. 14
- Slides: 14