1 / 14

AI: Exercise 4

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

kioko
Télécharger la présentation

AI: Exercise 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AI: Exercise 4 More About Lisp

  2. Functions on Lists (setf x '(a b c)) => (A B C) (setf y '(1 2 3)) => (1 2 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 Others: = tree-equal char-equal string-equal

  4. Functions on Tables

  5. Functions on Trees

  6. Functions on Numbers Functions on Sets Others: logior, bit-ior logandc2, bit-andc2 logbitp, bit logcount

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

  8. Data Types

  9. 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?

  10. Format

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

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

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

More Related