Simple vs complex user entry functions in Lisp -


at site: http://www.gigamonkeys.com/book/practical-a-simple-database.html there user entry function listed follows:

(defun prompt-read (prompt)     (format *query-io* "~%~a: " prompt)       (force-output *query-io*)     (read-line *query-io*)) 

are there major advantages of above function compared following simpler form:

(defun prompt-read2 (prompt)     (format t "~%~a: " prompt)     (setf answer (read-line))) 

is recommended use force-output , *query-io* time?

setting answer global variable bad. should return answer , let caller wants it. if use special (~global) variables, should put asterisks around name (*answer* instead of answer).

force-output needed ensure user sees prompt before having answer. if run second version using sbcl in terminal, program freezes wait input without saying anything.

*query-io* should used querying things user, because environment might want handle differently other output. example, might write gui wrapper program turns queries graphical dialogs. or maybe want run part of script, providing input string.

(defun prompt-read (prompt)   (format *query-io* "~%~a: " prompt)     (force-output *query-io*)   (read-line *query-io*))  (defun hello ()   (format t "~&hello ~a!~%" (prompt-read "what's name")))  (defmacro with-input ((input) &body body)   `(let ((*query-io* (make-two-way-stream (make-string-input-stream ,input)                                           (make-string-output-stream))))      ,@body))  (defun test ()   (with-input ("jkiiski")     (hello))   (with-input ("rnso")     (hello))) (test) ; hello jkiiski! ; hello rnso! 

edit

a more complex example using sbcls gray streams.

(defclass foo-stream (sb-gray:fundamental-character-input-stream)   ((output-input-script :initarg :script :accessor foo-stream-script)    (output-stream :initarg :out :accessor foo-stream-out)    (current-input :initform nil :accessor foo-stream-current-input)))  (defmethod sb-gray:stream-read-char ((stream foo-stream))   (with-accessors ((input foo-stream-current-input)                    (out foo-stream-out)                    (script foo-stream-script)) stream     (when (or (null input)               (not (listen input)))       (let ((output (string-trim '(#\space #\newline)                                  (get-output-stream-string out))))         (setf input (make-string-input-stream                      (format nil "~a~%"                              (cdr (assoc output script :test #'string=)))))))     (read-char input)))  (defun prompt-read (prompt)   (format *query-io* "~%~a: " prompt)     (force-output *query-io*)   (read-line *query-io*))  (defun hello ()   (format t "~&hello ~a!~%" (prompt-read "what's name"))   (format t "~&i'm ~a too!" (prompt-read "how you"))   (format t "~&~a~%" (if (string-equal (prompt-read                                         "do want delete files")                                        "yes")                          "deleting files... (not really)"                          "not deleting anything.")))  (defmacro with-input-script ((script) &body body)   (let ((out-sym (gensym "out")))     `(let* ((,out-sym (make-string-output-stream))             (*query-io* (make-two-way-stream                          (make-instance 'foo-stream                                         :out ,out-sym                                         :script ,script)                          ,out-sym)))        ,@body)))  (defun test ()   (with-input-script ('(("what's name:" . "jkiiski")                         ("how you:" . "great")                         ("do want delete files:" . "no")))     (hello))   (with-input-script ('(("what's name:" . "foo bar")                         ("how you:" . "fine")                         ("do want delete files:" . "yes")))     (hello)))  (test) ; hello jkiiski! ; i'm great too! ; not deleting anything. ; hello foo bar! ; i'm fine too! ; deleting files... (not really) 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -