Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input/Output Chapters 8 & 9. Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both.

Similar presentations


Presentation on theme: "Input/Output Chapters 8 & 9. Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both."— Presentation transcript:

1 Input/Output Chapters 8 & 9

2 Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both use same extra arguments as read > (list (read-char) (read-line)) This is my input (#\T “his is my input”)

3 Characters n Single characters printed with #\ in front –#\Y is the character capital Y –#\* is the character asterisk n Special characters have names –#\Space is the space character –#\Newline & #\Tab likewise –#\Asterisk = #\*

4 Character Input Return Values n Read-char and read-line return two values –first is the string/character read –second (T or NIL) says whether there’s more data after that in the file n Normally only the first value is used –need special function to get other value(s) –but both values print

5 Character Equality n Characters are equal only if same case n Char-equal does case-insensitive compare > (equal #\A #\A) T > (equal #\a #\A) NIL > (char-equal #\a #\A) T

6 String Equality n Strings are equal only if same case n String-equal does case-insensitive compare > (equal “ABC” “ABC”) T > (equal “abc” “ABC”) NIL > (string-equal “abc” “ABC”) T Actually just non-NIL

7 Substrings n Search looks for substring –returns position of substring (0 = 1 st position) –returns NIL if it’s not there –(search “abc” “abcdef”) => 0 –(search “cde” “abcdef”) => 2 –(search “fgh” “abcdef”) => NIL –(search “BC” “abcdef”) => NIL –(search “BC” “abcdef” :test #’char-equal) => 1

8 Converting to String n Search requires string arguments –won’t find a character in a string n Convert character to string using string (search (string #\d) “bad dog”) => 2 n String also works on atoms (string ‘fred) => “FRED” –doesn’t work on lists

9 Strings & Lists n Many list functions work on strings –(length “abc”) => 3 –(reverse “abcd”) => “dcba” –(elt “abcd” 1) => b n Nth works on lists, but not strings –(nth 1 ‘(a b c d)) => b –(nth 1 “abcd”) => ERROR

10 Detecting End-of-File n Can get and use second value (pp. 411–3) –tells you before you get there n Can do it the same way as read –add extra arguments for return value (read-line inStr nil ‘EOF) => EOF if inStr empty (read-char inStr nil ‘end) => END if inStr empty

11 Exercise n Write a function that scans a text file & prints out the lines containing the string “his” (or “His”, or “HIS”, …) –the name of the text file is given –prefix each line printed with its line number Hello. I’m writing to tell you his story. This may take a while. 3: his story. 4: This may take a while. (scan-for-his “input.txt”) input.txt

12 Solution Loop n Keep a counter for the line number –initialized to 1, updated by adding 1 each time n Read a line of input into a variable –update each time by reading next line –loop stops when we get end-of-file n Search it for “his” –if found, write out the line # and line

13 Solution (defun print-his-lines-from-file (fileName) (with-open-file (in fileName :direction :input) (do ((line-num 1 (+ 1 line-num)) (line (read-line in nil ‘eof) (read-line in nil ‘eof))) ((eql line ‘eof)) (when (search “his” line :test #’char-equal) (format t “~a: ~a~%” line-num line) ))))

14 Character Output n Write-char and write-line output characters and lines as you would normally like them > (write-char #\a) aNIL > (write-line “This is a string.”) This is a string. NIL Write-char just puts out the character Write-line adds a trailing new-line

15 Next Time n Repetition controls in LISP


Download ppt "Input/Output Chapters 8 & 9. Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both."

Similar presentations


Ads by Google