[racket] Printing multiple stmts in cond

From: nikhil (nik.mis at gmail.com)
Date: Sat Apr 30 03:41:36 EDT 2011

Thanks Neil and Richard !

Now that I look at your solutions, it looks so straightforward. Can't
believe it didn't occur to me.



On Sat, Apr 30, 2011 at 7:31 AM, Neil Van Dyke <neil at neilvandyke.org> wrote:

> 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/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110430/e2072b6f/attachment.html>

Posted on the users mailing list.