;;; that's chapter 9 (format t "hello") ;; so here's difference with Elisp, format takes "destination" as first attribute ;; needs 't to write to screen, others to file or such ;; in Elisp returns a string, right? yes (format t "~&There are old pilots,~&and there are bold pilots,~&but there are no bold old pilots.") (defun draw-line (n) (cond ((zerop n) (format t "~%")) (t (format t "*") (draw-line (- n 1))))) (draw-line 5) (defun draw-box (height width) (cond ((zerop height) (format t "~%")) (t (draw-line width) (draw-box (- height 1) width)))) (draw-box 3 15) (defun draw-ttt-row (row) (labels ((as-drawable (cell-state) (if (null cell-state) " " cell-state))) (format t "~&~A|~A|~A~%" (as-drawable (first row)) (as-drawable (second row)) (as-drawable (third row))))) (draw-ttt-row '(X O nil)) (defun draw-ttt-board (table) (draw-ttt-row (first table)) (format t "-----~%") (draw-ttt-row (second table)) (format t "-----~%") (draw-ttt-row (third table))) (draw-ttt-board '( (x x nil) (nil o nil) (x o x))) ;;; the read function (defun my-square () (format t "Please type in a number: ") (let ((x (read))) (format t "~&The number ~S squared is: ~S" x (* x x)))) (my-square) (defun test-y-or-n () (if (yes-or-no-p "~&Answer yes or no") (format t "~&this is the yes response") (format t "~&this is no response"))) (test-y-or-n) ;; reading from the file (defun get-tree-data (path-string) (with-open-file (stream path-string) ; both absolute from ~ and relative work (let* ((tree-loc (read stream)) (tree-table (read stream)) (num-trees (read stream))) (format t "~&There are ~S trees on ~S." num-trees tree-loc) (format t "~&They are ~S" tree-table)))) (get-tree-data "./test-input.lisp") ; cool ;; so just dumped elisp structures can be extracted ;; that is very very convenient (defun save-tree-data (tree-loc tree-table num-trees) (with-open-file (stream "/usr/tmp/test-lisp-data.lisp" :direction :output) (format stream "~S~%" tree-loc) (format stream "~S~%" tree-table) (format stream "~S~%" num-trees))) (save-tree-data "Tbilisi" '((45 redwood) (22 oak) (43 maple)) 110) (get-tree-data "/usr/tmp/test-lisp-data.lisp") ;;; exercise - drawing graph of arbitrary function (defun draw-y (y point) (format t "~&") (format t (concatenate 'string (make-string y :initial-element #\ ) point))) (defun make-graph () (let ((requested-function (progn (format t "~&Function to graph?") (read))) (start-value (progn (format t "~&Starting x value?") (read))) (end-value (progn (format t "~&Ending x value?") (read))) (plotting-string (progn (format t "~&Plotting string?") (read)))) (loop for x from start-value to end-value do (draw-y (funcall requested-function x) plotting-string)))) (defun square (n) (* n n)) (square 7) (funcall #'square 4) (make-graph) ;; yups (draw-y 5 "****") (concatenate 'string (make-string 10 :initial-element #\ ) ".") (concatenate 'string "hello" " " "who")