[racket] Printing multiple stmts in cond

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Sat Apr 30 02:55:01 EDT 2011

nikhil wrote at 04/30/2011 01:45 AM:
>
> How do I return/print both ?
>

There are a few different ways.  Which you choose depends on what you 
are trying to do.  Here is a program that you can paste into DrRacket 
and run for some examples:

#lang racket

(display "\nYour original code:\n")
(let ((var `(make))
      (exp '(1 2)))
  (cond [(number? 2)
         `(hi ,var)
         `(bye ,exp)]))

(display "\nReturning a single list that includes the other two lists:\n")
(let ((var `(make))
      (exp '(1 2)))
  (cond [(number? 2)
         `((hi ,var)
           (bye ,exp))]))

(display "\nUsing \"values\" to return multiple values instead of just 
one (this is usually for advanced programmers only):\n")
(let ((var `(make))
      (exp '(1 2)))
  (cond [(number? 2)
         (values `(hi ,var)
                 `(bye ,exp))]))

(display "\nUsing \"printf\" to display values rather than return them 
from the expression:\n")
(let ((var `(make))
      (exp '(1 2)))
  (cond [(number? 2)
         (printf "~S\n" `(hi ,var))
         (printf "~S\n" `(bye ,exp))
         "this is the actual returned value"]))

(display "\nUsing \"log-debug\" to send values to the log (see DrRacket 
\"View -> Show Log\" menu item):\n")
(let ((var `(make))
      (exp '(1 2)))
  (cond [(number? 2)
         (log-debug (format "~S" `(hi ,var)))
         (log-debug (format "~S" `(bye ,exp)))
         "this is the actual returned value"]))


-- 
http://www.neilvandyke.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110430/49f9e05f/attachment.html>

Posted on the users mailing list.