MC logo

Input/Output Demo


CS 231 Lecture Examples

<< Download
(setq fred '(a b fred (+ 5 38 alice) mike (terry frank tuesday) 33))
(setq fred (cons 'qqq (append fred (cadddr fred) (reverse fred))))

;
; You can generate output with write.  The (terpri) (for terminate print
; I think) generates a newline.
(terpri)
(write 17) (terpri)
(write "hi there") (terpri)
(write 'mike) (terpri)
(write fred) (terpri)

;
; You can generate uglier output with 
(write fred :pretty nil)

;
; There are several options you can set.  Their defaults are the values
; of global variables.
*print-pretty*
(setq *print-pretty* nil)
*print-pretty*
(write fred) (terpri)
(setq *print-pretty* t)
*print-pretty*
(write fred) (terpri)
; Many others.

;
; The group of prin1, print, pprint, and princ are the same, but don't
; use the whole generalized option mechanism.  They also start by
; printing a newline.

;
; If you want to get rid of those stupid quotes on all your strings, you
; can use write-string.
(write-string "How are you today?") (terpri)
(write-string "How about a nice afternoon nap.") (terpri)

;
; If you like files, the following construct will open a file, bind the
; output stream to the name fred, run the command, then close the file again.
; This one writes the single string to the file.
(with-open-file (fred "fredfile" :direction :output)
    (write "Greetings, mike" :stream fred)
)

;
; The above calls have forms for writing to a specific stream.  The forms
; are not a big pile of consistancy.
(with-open-file (fred "fredfile2" :direction :output)
    (write "More greetings, mike" :stream fred)
    (print "I like Mike." fred)
    (terpri fred)
    (write-string "zzz" fred)
    (terpri fred)
)

;
; The function read reads a variable.  It wants a lisp expression.  Note
; that the data read is simply reaturned.  In this example, we use let*
; to assign it a value.
(write-string "Enter something: ")
(let*
    ((invar (read)))
    (progn
        (write-string "You entered: ")
        (print invar)
    )
)

;
; You can read from files, too.
(with-open-file (fred "fredfile")
    (print (read fred))
)

;
; If you want read characters, you can do a read-line.  Note that if you 
; entered something for the last entry which was terminated by ), 
; this won't wait for you, it will just read the rest of the line after
; the ).
(terpri)
(write-string "Enter a line: ")
(let*
    ((invar (read-line)))
    (progn
        (write-string "You entered: ")
        (print invar)
    )
)

;
; Or from a file...
(with-open-file (fred "fredfile2")
    (print (read fred))
    (print (read fred))
    (print (read fred))
)

;
; Make some spaces.
(defun n-spaces (n)
    (if (> n 0) (progn
        (write-string " ")
        (n-spaces (- n 1))
    ))
)

;
; Here's a function to print out an expression with lots of space.
(defun fancy-print (E) (fancy-print-r 0 E))
(defun fancy-print-r (ind E)
    (if (listp E)
        (progn ; progn just evaluates a list of expressions.  A lisp { }
            ; E is a list.
            (n-spaces ind) (write-string "(") (terpri)
            (mapcar (lambda (x) (fancy-print-r (+ ind 4) x)) E)
            (n-spaces ind) (write-string ")") (terpri)
        )
        (progn
            ; E is not a list.
            (n-spaces ind)
            (write E)
            (terpri)
        )
    )
)

;
; Then there's format.  It is like printf, but all the details are
; different.  It takes a destination (nil means return string, stream
; means print there, t means print to standard output), a format string,
; and arguments consumed by the format.  ~ does %.
(terpri)
(format t "So: ~A has ~5,'0D marbles." "Mike" 45)
(with-open-file (of "fredfile3" :direction :output)
    (format of "[~D]" 55)
    (terpri of)
)

<<