140 likes | 280 Views
AI: Exercise 4. More About Lisp. Functions on Lists (setf x '(a b c)) => (A B C) (setf y '(1 2 3)) => (1 2 3). Different equality functions eq – tests for the exact same object eql – tests for objects that are equivalent numbers
E N D
AI: Exercise 4 More About Lisp
Functions on Lists (setf x '(a b c)) => (A B C) (setf y '(1 2 3)) => (1 2 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 Others: = tree-equal char-equal string-equal
Functions on Numbers Functions on Sets Others: logior, bit-ior logandc2, bit-andc2 logbitp, bit logcount
Data Types (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 )
Input/Output (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?
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
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 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. 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
Exercise Problems If you have time, try to do the following exercise problems. • Write a function that can save (into a file) and print nine-nine table (九九表) in the table format. • Modify the example 2 (e.g.2: math-quiz) in lecture note so that users does not need to remember three arguments.