[racket] Printing multiple stmts in cond

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Sat Apr 30 03:31:00 EDT 2011

nikhil wrote at 04/30/2011 03:16 AM:
> In my case, I have an incoming function which takes 4 arguments and I 
> need to generate a function which takes in 5 args when a 
> condition satisfies, if not then the original 4 args .

 From looking at the example you provided, two ways come to mind 
immediately.  Either way is fine.  Which you use depends on the 
circumstances, including code maintainability.

#lang racket

(define (f? x)
  ;; This is just a placeholder for "f?".
  #t)

(define (condition?)
  ;; This is just a placeholder for "condition?".
  (zero? (random 2)))

;; This uses ",@" splicing.
(define (parse-func-1 exp)
  (let ((x1 'x1) (x2 'x2) (x3 'x3) (x4 'x4))
   (cond
     [(f? exp)
      `(FF
        ,x1
        ,x2
        ,@(if (condition?)
              '(xNEW)
              '())
        ,x3
        ,x4)])))

;; This moves the "if" higher up.
(define (parse-func-2 exp)
  (let ((x1 'x1) (x2 'x2) (x3 'x3) (x4 'x4))
   (cond
     [(f? exp)
      (if (condition?)
          `(FF
            ,x1
            ,x2
            xNEW
            ,x3
            ,x4)
          `(FF
            ,x1
            ,x2
            ,x3
            ,x4))])))


    
-- 
http://www.neilvandyke.org/


Posted on the users mailing list.