Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 More About Lisp. 2 Functions on Lists (setf x '(a b c)) => (A B C) (setf y '(1 2 3)) => (1 2 3) (rest x) => (B C) ;;all but the first element (nth 1.

Similar presentations


Presentation on theme: "1 More About Lisp. 2 Functions on Lists (setf x '(a b c)) => (A B C) (setf y '(1 2 3)) => (1 2 3) (rest x) => (B C) ;;all but the first element (nth 1."— Presentation transcript:

1 1 More About Lisp

2 2 Functions on Lists (setf x '(a b c)) => (A B C) (setf y '(1 2 3)) => (1 2 3) (rest x) => (B C) ;;all but the first element (nth 1 x) => B ;;nth element of a list (car x) => A ;;the first element of a list (cdr y) => (2 3) ;;all but the first name (reverse x) => (C B A) ;;puts list in reverse order (cons 0 y) => (0 1 2 3) ;;add to front of list (append x y) => (A B C 1 2 3) ;;append together elements (list x y) => ((A B C) (1 2 3)) ;;make a new list (list* x y) =>((A B C) 1 2 3) ;;append last argument to others (null nil) => T ;;it is true for the empty list (null x) => NIL ;;it is false for everything else (listp x) => T ;;it is true for any list (listp nil) => T ;;including nil list (listp 3) => NIL ;;it is false for non-list (consp x) => T ;;it is true for non-nil list (consp nil) => NIL ;;it is false for atoms, including nil (equal x y) => NIL ;;false for lists that looks different (equal x '(a b c)) => T ;;true for lists that look the same (sort y #'>) => (3 2 1) ;;sort the list according to > function (subseq x 1 2) => (B) ;;subsequence with given start and end

3 3 Different equality functions eq – tests for the exact same object 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 strict x y eq eql equal equalp ‘x ‘x ‘0 ‘0 ‘(x) ‘(x) ‘”xy” ‘”xy” ‘”Xy” ‘”xY” ‘0 ‘0.0 T T T T nil nil T T nil nil nil T Others: = tree-equal char-equal string-equal

4 4 Functions on Tables ;; table representation (setf capitalCity-table ‘ ((UK. London) (JP. Tokyo) (CN. Beijing) (UAS. Washingto n DC)) ) ((UK. LONDON) (JP. TOKYO) (CN. BEIJING) (UAS. WASHINGTONDC)) (assoc 'JP capitalCity-table) => (JP. TOKYO) (cdr (assoc 'JP capitalCity-table)) => TOKYO (assoc 'FR capitalCity-table) => NIL (rassoc 'Tokyo capitalCity-table) => (JP. TOKYO) ;; hash table representation (setf hashTable (make-hash-table)) => # (setf (gethash 'UK hashTable) 'London) => LONDON (setf (gethash 'JP hashTable) 'Tokyo) => TOKYO (setf (gethash 'CN hashTable) 'BeiJing) => BEIJING (setf (gethash 'USA hashTable) 'WashingtonDC) => WASHINGTONDC (gethash 'UK hashTable) => LONDON T (gethash 'FR hashTable) => NIL NIL ;;property list (property/value) representation (setf (get 'UK 'capital) 'London) => LONDON (setf (get 'JP 'capital) 'Tokyo) => TOKYO (setf (get 'CN 'capital) 'BeiJing) => BEIJING (setf (get 'USA 'capital) 'WashingtonDC) => WASHINGTONDC (get 'UK 'capital) => LONDON (get 'FR 'capital) => NIL

5 5 Functions on Trees (setf aTree '((a b) ((c)) (d e))) ((A B) ((C)) (D E)) (tree-equal aTree '((a b) ((c)) (d e))) => T (tree-equal aTree '((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 aTree '((1 2) ((3)) (4 5 ))) => T (same-shape-tree aTree '((1 2) ((3)) (4 5 6))) => NIL (subst '1 ‘c aTree) ((A B) ((1)) (D E)) (sublis '((a. 1)) aTree) ((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?)

6 6 Functions on Numbers (+ 4 2) => 6 (- 4 2) => 2 (* 4 2) => 8 (/ 4 2) => 2 (> 100 99) => T (= 100 101) => NIL ( 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 (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 #b11110 #b11001) => 24 Functions on Sets Others: logior, bit-ior logandc2, bit-andc2 logbitp, bit logcount

7 7 Data Types Type Example Explanation character # c number 42 float 3.14 integer 24 fixnum 123 An integer that fits in a single word of storage bignum 123456789 An integer of unbounded size function #’sin A function can be applied to an argument list symbol sin Symbols can name fns and vars, and are themselves objects null nil keyword :key Keywords are a subtype of symbol sequence (a b c) Including lists and vectors list (a b c) A cons or null vector #(a b c) a subtype of sequence cons (a b c) a non-nil list atom t anything that is not cons string “a b c” array #1A(a b c) vectors and higher-dimension arrays structure #S(type …) Structures are defined by defstruct hash-table It is created by make-hash-table (setf ar #1A(a b c)) => #(A B C) (aref ar 1) => B (setf ar #2A((a b c) (d f g))) #2A((A B C)(D F G)) (aref ar 1 2) => G (defstruct name first last) NAME (setf st #S(name :first rose :last huang)) #S( NAME :FIRST ROSE :LAST HUANG )

8 8 Data Types Type Example Explanation t 52 Every object is of type t nil No object is of type nil complex #C(0 1) Imaginary numbers bit 0 0 or 1 rational 2/3 Including integers and ratios ratio 2/3 Exact fractional numbers simple-array #1A(x y) An array that is not displaced or adjustable readtable A mapping from characters to their meanings to read package A collection of symbols that form a module pathname #P”/usr/spool/mail” A file or directory name stream A pointer to an open file random-state A state used as a seed by random

9 9 Input/Output print prints any object on a new line. prin1 prints any object without the new line and space. princ is used to print in a human-readable format. read for print and prin1, 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 prin1 or princ. (with-open-file (stream "test.text" :direction :output) (print '(hello there) stream) (princ 'goodbye stream)) GOODBYE (with-open-file (stream "test.text":direction :input) (list (read stream) (read-char stream) (read-char stream) (read-char stream) (read stream) (read stream nil 'eof))) ((HELLO THERE) G O ODBYE EOF) In file, test.text: (HELLO THERE) GOODBYE If you insert a line here (terpri stream) What is result in test.text file?

10 10 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.

11 11 Examples of format : (format t "Hello, how are you?") two plus "two" is 4.0NIL (format t "~&~a plus ~s is ~f" "two" "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.0NIL

12 12 (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 ctemp)) ;; return ratio value F-TO-C (f-to-c) Please enter Fahrenheit temperature: (100) (100) degrees Fahrenheit is 37.77778 degrees Celsius 340/9 e.g. 1: temperature converter

13 13 (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 e.g. 2: math quiz

14 14 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.


Download ppt "1 More About Lisp. 2 Functions on Lists (setf x '(a b c)) => (A B C) (setf y '(1 2 3)) => (1 2 3) (rest x) => (B C) ;;all but the first element (nth 1."

Similar presentations


Ads by Google