input and output input and output in lisp
play

Input and Output Input and Output in LISP York University CSE 3401 - PowerPoint PPT Presentation

Input and Output Input and Output in LISP York University CSE 3401 Vida Movahedi 1 York University CSE 3401 V. Movahedi 16_LISP_IO Overview Overview Read and Print Escape characters in symbol names Escape characters in symbol


  1. Input and Output Input and Output in LISP York University CSE 3401 Vida Movahedi 1 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  2. Overview Overview • Read and Print • Escape characters in symbol names Escape characters in symbol names • Strings & how to format them • File I/O [ref.: Chap. 10 ‐ Wilensky ] 2 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  3. Read Read • (read) ( d) – Read can be used a function of no arguments – It reads one s ‐ expression from the standard input It reads one s expression from the standard input – And returns it. > (setq x (read)) ( t ( d)) > y 20 � input by user (A B) 20 > x > (setq z (cons ‘a (read)) 20 (b c) � input by user ( ) p y (A B C) > (setq y (read)) (a b) � input by user > z (A B) (A B C) 3 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  4. Print Print • (print arg) – Print can be used a function with one argument – The one argument must be an s ‐ expression Th t t b i – It prints to the standard output, • A new line A new line • Then its argument • Then a single space – Returns its argument R t it t > (print ‘enter) > (print enter) � new line ѣ � single space ENTER ѣ PRINTED ENTER RETURNED 4 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  5. Print (cont ) Print (cont.) • Example: > ((lambda () (print ‘enter) (setq x (read)))) ‐ blank line bl k li ENTER 10 ‐ 10 entered by user 10 10 ‐ 10 returned by the last form, (setq ...) 10 returned by the last form, (setq ...) > (let () (print ‘enter) (print ‘a) (print ‘number) (setq x (read))) ‐ blank line ENTER A NUMBER 20 ‐ 20 entered by user 20 20 ‐ 20 returned by the last form, (setq ...) 20 returned by the last form, (setq ...) 5 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  6. Prin1 Terpri Prin1, Terpri • (prin1 arg) ( i 1 ) – Only prints its argument (no new lines or spaces) – Returns its argument g • (terpri) – Stands for “terminate print line” p – Prints a carriage return (new line) – Returns NIL Part of symbol’s name y > ( prog () (prin1 'enter>) (if (numberp (read)) (prin1 'ok) (prin1 'Nop!)) (if (numberp (read)) (prin1 ok) (prin1 Nop!)) (terpri)) Value returned by ENTER> 11 terpri? No prog returns terpri? No, prog returns OK OK NIL when done. NIL 6 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  7. Example (1) Example (1) > (loop (l (print ‘number>) (let ((in (read))) (if (equal in ‘end) (return nil)) (print (sqrt in)) )) ѣ NUMBER> ѣ 25 ѣ 5 ѣ 5 ѣ NUMBER> ѣ 9 ѣ 3 ѣ 3 ѣ NUMBER> ѣ end NIL 7 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  8. Example (2) Example (2) > (loop (print ‘(A number please>)) (return (read))) (return (read))) ѣ Prints the parenthesis! (A NUMBER PLEASE>) ѣ 20 ( ) 20 > (let () (l () (mapc ‘prin1 ‘(A number please>)) (read)) (read)) No spaces! ANUMBERPLEASE>10 10 8 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  9. Escape characters Escape characters • Any way to add a space?! YES! • Method 1 : Add a space to symbol’s name Method 1 : Add a space to symbol s name – \ (single escape character): allows the character following it to escape the normal LISP interpretation it to escape the normal LISP interpretation – | (multiple escape character): anything between a pair of vertical bars escapes the normal LISP interpretation vertical bars escapes the normal LISP interpretation � waits for the closing parenthesis (setq ab(c 10) � sets the value of the symbol ab(c (setq ab\(c 10) 9 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  10. Escape characters (cont ) Escape characters (cont.) > (setq |a var| 10) Can have spaces in 10 symbol’s name > |a var| > |a var| 10 > (setq |BigVar| 200) > (setq |BigVar| 200) 200 > |BigVar| | g | 200 > ‘BigVar > BigVar No changing to UPPER BIGVAR CASE, if escape > ‘|BigVar| characters used characters used. |BigVar| 10 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  11. Example (3) Example (3) > (let () (mapc ‘prin1 ‘(a | ѣ number ѣ | please\ ѣ >)) (read)) (read)) Prints the escape A| ѣ number ѣ ||PLEASE ѣ >| characters!!! Note PLEASE is in UPPER CASE, but number is not. CASE but number is not > (print ‘|A ѣ number ѣ please ѣ >|) ѣ ѣ |A number please >| |A number please >| | p | 11 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  12. princ princ • Is there a way to print anything looking nice?! YES ☺ Use ?! YES ☺ U I h i hi l ki i princ : > (prog () (princ ‘|A number please> |) (read)) > (prog () (princ |A number please> |) (read)) A number please> 100 NIL • The return value of print and princ are the same, only the printed output is different. > (princ ‘|A ѣ number ѣ please ѣ >|) A number please > |A number please >| | p | • Princ does NOT prints s ‐ expression , but prints in human ‐ readable format. If what is being printed needs to be read or readable format. If what is being printed needs to be read or used by LISP use print to print s ‐ expressions 12 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  13. Strings Strings • Other data types, such as strings have been added to LISP to increase functionality • A string is a sequence of characters enclosed in double quotes e g “Hello there!” double quotes, e.g. Hello there! • Method 2: Use strings > ((lambda () (princ “A number please: “) (read))) A number please: 100 100 – We still need to use princ for not having the double quotes We still need to use princ for not having the double quotes 13 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  14. Strings (cont ) Strings (cont.) > “Hello there!” “Hello there!” > (print “Hi”) ѣ Printed value & Printed value & “Hi” Returned value “Hi” > (princ “Hi”) Hi “Hi” Printed value & Returned value 14 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  15. Strings (cont ) Strings (cont.) • A symbol’s name (also called print name) is a string. A b l’ ( l ll d i ) i i > (symbol ‐ name ‘x) “X” “X” > (symbol ‐ name ‘BigVar) “BIGVAR” BIGVAR > (symbol ‐ name ‘ab\(c) “AB(C” > (symbol ‐ name ‘|A Big Var|) “A Big Var” • Strings don’t have components (values, property lists, etc), therefore require less storage space 15 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  16. Format Format • (format destination string ....) (f d i i i ) – Destination: • Nil: just return the formatted string Nil: just return the formatted string • T: to standard output • Any other stream – String (can contain directives) ~A or ~nA Prints one argument as if by PRINC ~S or ~nS Prints one argument as if by PRIN1 f b ~D or ~nD Prints one argument as a decimal integer ~F or ~nF Prints one argument as a float g ~O,~B, ~X Prints one argument as an octal, binary, or hexidecimal ~% Does a TERPRI where n is the width of the field in which the object is printed h i th idth f th fi ld i hi h th bj t i i t d 16 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  17. Format (cont.) Format (cont ) > (setq n 32) > (setq n 32) > (format nil "N is ~d~%" n) (f t il "N i d %" ) 32 "N is 32 > (format t "N is ~d" n) “ N is 32 N i 32 > (format nil "N is ~7,2f" n) NIL “N is 32.00” > (format nil "N is ~d" n) > (format nil "Hi ~a" "Bob") > (f t il "Hi ~ " "B b") " "N is 32“ “ "Hi Bob“ > (format nil "N is ~5d" n) “N is 32” > (format nil "Hi ~s" "Bob") ( ) > (format nil "N is ~10b" n) "Hi \"Bob\"“ “N is 100000” > (format nil "Hi ~s" '|Bob|) > (format nil N is 10, 0b n) > (format nil "N is ~10,'0b" n) "Hi |Bob|“ Hi |Bob| “N is 0000100000” > (format nil "Hi ~a" '|Bob|) > (format nil "N is ~:b" n) "Hi Bob" “N is 100 000” N is 100,000 17 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  18. Files Files P th Path and Filename as string d Fil t i • Writing to files > (setq outstream (open “c:\\data.txt” :direction :output)) #<OUTPUT BUFFERED FILE STREAM CHARACTER #P"C \\data txt"> #<OUTPUT BUFFERED FILE ‐ STREAM CHARACTER #P"C:\\data.txt"> > (print ‘(1 2 3) outstream) :keywords (1 2 3) Opening for output Opening for output > (close outstream) ( l t t ) T • Reading from files R di f fil > (setq instream (open “/usr/lisp/file.dat” :direction :input)) #<INPUT BUFFERED FILE ‐ STREAM CHARACTER #P"C:\\lispcode\\file.txt" @1> \\ p \\ @ > (read instream) (1 2 3) > (close instream) (close instream) T 18 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

  19. Files (cont ) Files (cont.) • What happens when reaching end of file? > (read instream) Error ‐ going beyond end of file! > (read instream nil ‘eof) EOF EOF >(read instream nil ‘oops) OOPS OOPS (read stream eof ‐ error ‐ p eof ‐ value) If eof ‐ error ‐ p is T, generates error if eof reached generates error if eof reached. If eof ‐ error ‐ p is NIL, returns eof ‐ value if eof reached. 19 York University ‐ CSE 3401 ‐ V. Movahedi 16_LISP_IO

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend