(my-square) Please type a number: 2 The number 2 squared is 4. NIL"> (my-square) Please type a number: 2 The number 2 squared is 4. NIL">
Download presentation
Presentation is loading. Please wait.
Published byBrianne Pope Modified over 8 years ago
1
Input Streams “A program designed for inputs from people is usually stressed beyond the breaking point by computer-generated inputs.” Dennis Ritchie, Bell Labs
2
Reading Primary reading function is read Reads one S-expression from the currently selected input device and returns it as a value Only uses first expression >(read) 99 >(read) (a b c) (A B C) >(read) 1 2 3 1 > 2 > 3
3
Read Example (1) >(defun my-square () (format t "Please type a number: ") (let ((x (read))) (format t "The number ~A squared is ~A.~%" x (* x x)))) MY-SQUARE >(my-square) Please type a number: 2 The number 2 squared is 4. NIL
4
Read Example (2) >(defun wage-calc () (format t "Please enter your hourly wage: ") (let ((wage (read))) (format t "Please enter hours worked: ") (let ((hours (read))) (format t "Your pay is: $~$" (* wage hours))))) WAGE-CALC >(wage-calc) Please enter your hourly wage: 3 Please enter hours worked: 5 Your pay is: $15.00 NIL
5
Other Read Functions read-char reads any character, including newlines – Returns character read read-line reads an entire line up to and including the newline character – Returns a string with the characters read, not including the newline
6
Peek-char You can look at the next character in a stream without advancing the stream (peek-char mode stream) If mode is nil, then you always get the next character in the stream If mode is t, then peek-char will actually advance the stream past white space to peek at the next character that’s not white space
7
String Input Stream make-string-input-string converts a string into an input stream >(defvar str (make-string-input-stream "one two three")) STR >(read-char str) #\o >(read-char str) #\n >(read-char str) #\e >(read str) TWO >(peek-char nil str) #\Space >(read-char str) #\Space >(peek-char nil str) #\Space >(peek-char t str) #\t >(read str) THREE
8
End of Stream listen tests whether there is an available character in a stream By default, all read functions signal an error if there is nothing left to read Giving these read functions a second argument of nil makes them return NIL instead >(read (make-string-input-stream "") nil) NIL Giving yet another argument allows you to specify what value to return when the end of the stream is hit >(read (make-string-input-stream "") nil 'EOS) EOS
9
More Input Stream Functions clear-input flushes the contents of a stream open-stream-p tests if a stream is open close turns off a stream
10
File IO Open streams to files using the open function (open stream-name :direction :input) (open stream-name :direction :output) open returns a stream stream-name is usually a quoted file path probe-file checks to see if a file exists
11
File IO Example >(probe-file "test.txt") NIL >(defvar str (open "test.txt" :direction :output)) STR >(probe-file "test.txt") #p"/v/filer4b/v20q001/schrum2/test.txt" >(format str "We can write anything!~%") NIL >(close str) T
12
With-Open-File Remembering to close open streams can be cumbersome, so there is a shortcut (with-open-file (sym filename) S-expr*) Automatically closes the file when control leaves the expression Can take a :direction of :input or :output Can also specify what to do if file already exists, e.g. :if-exists :supersede
13
With-Open-File Example (defun print-name-help (str name) (format str "My name is ~A" name)) (defun print-name (filename) (with-open-file (str filename :direction :output :if-exists :supersede) (format t "Enter your name: ") (let ((name (read))) (print-name-help str name))))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.