From: Emilio Lopes Newsgroups: comp.lang.scheme Subject: Re: simple print question Date: Sun, 25 Jun 2006 12:49:01 +0200 Organization: The Church of Emacs User-Agent: Emacs Gnus Joey Fox writes: > I just want to use a print statement with multiple things inside of it. > This doesn't seem possible. Is there a work-a-round? > (print "the value of variable foo is " foo) > If this debugging task cannot be done with a print statement, is there > any other way to do it? Here is what I use. It WRITEs its arguments, proceeded by "### " (so that you can easily recognise it) and returns the value of the last one. It's a macro, so that you can use it with more complex expressions. (define-syntax debug (syntax-rules () ((_ expr) (let ((literal (quote expr)) (value expr)) (display "### ") (display literal) (display ": ") (write value) (newline) value)) ((_ expr expr1 ...) (begin (debug expr) (debug expr1 ...))))) Suppose you need to debug this function: (define (my-length lst) (let loop ((lst lst) (len 0)) (if (null? lst) len (loop (cdr lst) (+ len 1))))) Because the DEBUG macro above generates code which just returns its argument, you can simply wrap the expressions you want to see into a call to DEBUG: (define (my-length lst) (let loop ((lst lst) (len 0)) (if (null? lst) len (loop (debug (cdr lst)) (debug (+ len 1)))))) The output looks like this: > (my-length '(a b c)) ### (cdr lst): (b c) ### (+ len 1): 1 ### (cdr lst): (c) ### (+ len 1): 2 ### (cdr lst): () ### (+ len 1): 3 3 > -- Emílio C. Lopes Munich, Germany