No, this is not about L'OBJET, why you should hate C++, or
finding jobs. Amazingly, it is about Lisp :-).
Anyhow, another instructor passed along to me a request from a student
who was looking for a way to save test cases in a file, but then get a
printout of both the test case and the evaluated result. Here's a
simple hack that does that. Although easy, it still once again
illustrates the advantages of having macros in the language.
- Marty
(proclaim '(inline skates))
;;;========================================================================
(in-package :User)
;;;=========================================================================
;;; A mini package for showing the results of test cases.
;;; Eg:
;;; (Show-Output
;;; (+ 2 3)
;;; (list 'A 'B 'C)
;;; (cons 'A (list (* 6 7))))
;;;
;;; > (+ 2 3)
;;; 5
;;;
;;; > (LIST 'A 'B 'C)
;;; (A B C)
;;;
;;; > (CONS 'A (LIST (* 6 7)))
;;; (A 42)
;;;
;;; 2/95 Marty Hall. ··········@jhuapl.edu
;;;=========================================================================
;;;=========================================================================
;;;=========================================================================
(defmacro Show-Output (&body Forms)
"Takes a number of list forms, and prints out each form and its resultant
value. Prints a '>' in front of the form, since '>' is a common prompt"
`(progn ,@(apply #'append (mapcar #'Print-and-Eval Forms))
(values)))
;;;=========================================================================
;;; Same as Show-Output, but the first argument must be a stream to which
;;; to direct the printout.
(defmacro Show-Output-on-Stream (Stream &body Forms)
"Same as Show-Output, but the first arg must be a stream to which to direct
the printout."
`(progn ,@(apply #'append (mapcar #'(lambda (Form)
(Print-and-Eval Form Stream))
Forms))
(values)))
;;;=========================================================================
(defun Print-and-Eval (Form &optional (Stream t))
`((let ((*print-pretty* t))
(format ,Stream "> ~S" ',Form))
(pprint (eval ',Form) ,Stream)
(terpri ,Stream)
(terpri ,Stream)))
;;;=========================================================================