Re: [racket] Printing multiple stmts in cond

2011-04-30 Thread John Clements

On Apr 30, 2011, at 12:41 AM, nikhil wrote:

> Thanks Neil and Richard !
> 
> Now that I look at your solutions, it looks so straightforward. Can't believe 
> it didn't occur to me. 

If I might interject a meta-comment: your use of the word "statement" (or, more 
precisely, "stmt") in your subject line explains most of the difficulty you 
were having; in expression-poor imperative languages, you have to "get things 
done" by using a sequence of statements to cause a series of mutations to the 
program state.  In functional programming, that's not necessary.  

In any case, I'm delighted to see that the functional solutions make sense to 
you!

John "fp cheerleader" Clements



smime.p7s
Description: S/MIME cryptographic signature
_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users

Re: [racket] Printing multiple stmts in cond

2011-04-30 Thread nikhil
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  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/
>
_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users

Re: [racket] Printing multiple stmts in cond

2011-04-30 Thread Neil Van Dyke

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/
_
 For list-related administrative tasks:
 http://lists.racket-lang.org/listinfo/users


Re: [racket] Printing multiple stmts in cond

2011-04-30 Thread Richard Cleis
(if (condition? data)
(FF x1 x2 xNEW x3 x4)
(f x1 x2 x3 x4))

The result of one of them is returned.

rac


On Apr 30, 2011, at 1:16 AM, nikhil wrote:

> Ok. Here's what I am trying to do.
> 
> I am parsing incoming racket code and generating back racket code. 
> 
> 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 .
> 
> Something like,
> 
> Input--> 
> (f x1 x2 x3 x4)
> 
> Output-->
> (FF x1 x2 xNEW x3 x4)
> 
> --
> 
> (define (parse-func exp)
>(cond
> 
> 
>[(f? exp)
>`(FF
>  ,x1
>  ,x2
>  ,(if (condition?)
>   xNEW)
>
>  ,x3
>  ,x4)]
> 
> (The above is just pseudo-code) As shown above, when the if condition fails I 
> just want 4 args. But a default # is passed instead when used with 
> cond. 
> 
> I am new to racket and functional programing in general, so I am not able to 
> express my problem very accurately :(
> Hope the above example is clear enough to explain what I am doing. 
> 
> 
> 
> On Sat, Apr 30, 2011 at 7:00 AM, Neil Van Dyke  wrote:
> nikhil wrote at 04/30/2011 02:52 AM:
> 
> Basically, I dont want to execute an else stmt or return multiple values. I 
> just want to return "stmt1" if condition satisfies and dont do anything if 
> false. No returning # or anything.
> 
> An expression always produces at least one value, even if the value is the 
> void value.  (Usually, a REPL will hide a returned void value from you, but a 
> void value is still being returned, which can be confusing.)
> 
> If you can show *why* you don't want to produce *any* value, then people can 
> show you different ways to write that code.
> 
> -- 
> http://www.neilvandyke.org/
> 
> _
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users

_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users

Re: [racket] Printing multiple stmts in cond

2011-04-30 Thread Richard Cleis

On Apr 30, 2011, at 12:52 AM, nikhil wrote:

> What you guys suggest solves the problem of returning multiple values. Since 
> I havent provided any context, my original problem still exists. Sorry about 
> that.
> 
> This is what I was trying to do,
> 
> (if (condition?) 
>  (stmt1))
> 
> (if (condition?)
>  (stmt2)
>  (stmt3))
>
> In the first if condition(stmt1), I have no else condition. Since this is not 
> allowed, I used (cond ...)

For conditionals with only one clause, use (when ... ...) or (unless ... ...)

> Basically, I dont want to execute an else stmt or return multiple values. I 
> just want to return "stmt1" if condition satisfies and dont do anything if 
> false. No returning # or anything.
> 
> Am I making any sense ? if yes, is it possible ? 
> 

> 

What does it mean, to return nothing? 'if' will return the results of one of 
it's two clauses. However, a when statement will either return the result of 
it's only clause or something that represents nothing: void. A program can 
check for the result of void like any other value.


rac

_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users


Re: [racket] Printing multiple stmts in cond

2011-04-30 Thread nikhil
Ok. Here's what I am trying to do.

I am parsing incoming racket code and generating back racket code.

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 .

Something like,

Input-->
(f x1 x2 x3 x4)

Output-->
(FF x1 x2 xNEW x3 x4)

--

(define (parse-func exp)
   (cond


   [(f? exp)
   `(FF
 ,x1
 ,x2
 ,(if (condition?)
  xNEW)

 ,x3
 ,x4)]

(The above is just pseudo-code) As shown above, when the if condition fails
I just want 4 args. But a default # is passed instead when used with
cond.

I am new to racket and functional programing in general, so I am not able to
express my problem very accurately :(
Hope the above example is clear enough to explain what I am doing.



On Sat, Apr 30, 2011 at 7:00 AM, Neil Van Dyke  wrote:

> nikhil wrote at 04/30/2011 02:52 AM:
>
>  Basically, I dont want to execute an else stmt or return multiple values.
>> I just want to return "stmt1" if condition satisfies and dont do anything if
>> false. No returning # or anything.
>>
>
> An expression always produces at least one value, even if the value is the
> void value.  (Usually, a REPL will hide a returned void value from you, but
> a void value is still being returned, which can be confusing.)
>
> If you can show *why* you don't want to produce *any* value, then people
> can show you different ways to write that code.
>
> --
> http://www.neilvandyke.org/
>
_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users

Re: [racket] Printing multiple stmts in cond

2011-04-30 Thread Neil Van Dyke

nikhil wrote at 04/30/2011 02:52 AM:
Basically, I dont want to execute an else stmt or return multiple 
values. I just want to return "stmt1" if condition satisfies and dont 
do anything if false. No returning # or anything.


An expression always produces at least one value, even if the value is 
the void value.  (Usually, a REPL will hide a returned void value from 
you, but a void value is still being returned, which can be confusing.)


If you can show *why* you don't want to produce *any* value, then people 
can show you different ways to write that code.


--
http://www.neilvandyke.org/
_
 For list-related administrative tasks:
 http://lists.racket-lang.org/listinfo/users


Re: [racket] Printing multiple stmts in cond

2011-04-29 Thread Neil Van Dyke

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/
_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users

Re: [racket] Printing multiple stmts in cond

2011-04-29 Thread nikhil
What you guys suggest solves the problem of returning multiple values. Since
I havent provided any context, my original problem still exists. Sorry about
that.

This is what I was trying to do,

(if (condition?)
 (stmt1))

(if (condition?)
 (stmt2)
 (stmt3))

In the first if condition(stmt1), I have no else condition. Since this is
not allowed, I used (cond ...) which returns # by default when there's
no else. This # is being somehow copied to the returned list.

So I tried clubbing multiple return statements in cond stmt because I had no
else condition.

Basically, I dont want to execute an else stmt or return multiple values. I
just want to return "stmt1" if condition satisfies and dont do anything if
false. No returning # or anything.

Am I making any sense ? if yes, is it possible ?



On Sat, Apr 30, 2011 at 6:28 AM, Richard Cleis  wrote:

> You can return more than one by forming a list, or by using something like
> (values val-1 val-2 ...)
>
> rac
>
> On Apr 29, 2011, at 11:45 PM, nikhil wrote:
>
> Hi,
>
> I am new to Racket so please bear with me. I am trying to print multiple
> expressions in "cond" statement as below.
>
> (let ((var `(make))
>
> (exp '(1 2)))
>
> (cond
>
>   [(number? 2) `(hi ,var)
>
>   `(bye ,exp)]))
>
>
>
> But only the "bye" statement is returned/printed on the screen. The first
> "hi" is not evaluated at all. How do I return/print both ?
>
> Thanks
>
>
> Nik
> _
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
>
>
>
_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users

Re: [racket] Printing multiple stmts in cond

2011-04-29 Thread Richard Cleis
You can return more than one by forming a list, or by using something like 
(values val-1 val-2 ...)

rac

On Apr 29, 2011, at 11:45 PM, nikhil wrote:

> Hi,
> 
> I am new to Racket so please bear with me. I am trying to print multiple 
> expressions in "cond" statement as below.
> 
> 
> (let ((var `(make))
> 
> (exp '(1 2)))
> 
> (cond
> 
>   [(number? 2) `(hi ,var)
> 
>   `(bye ,exp)]))
> 
> 
> 
> 
> But only the "bye" statement is returned/printed on the screen. The first 
> "hi" is not evaluated at all. How do I return/print both ?
> 
> Thanks
> 
> 
> 
> Nik
> 
> _
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users

_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users

Re: [racket] Printing multiple stmts in cond

2011-04-29 Thread Eli Barzilay
Three minutes ago, nikhil wrote:
> Hi,
> 
> I am new to Racket so please bear with me. I am trying to print
> multiple expressions in "cond" statement as below.
> 
> (let ((var `(make))
> (exp '(1 2)))
> (cond
>   [(number? 2) `(hi ,var)
>   `(bye ,exp)]))
> 
> But only the "bye" statement is returned/printed on the screen.  The
> first "hi" is not evaluated at all.

It is, it's just not printing anything or doing anything else since
its value is ignored.


> How do I return/print both ?

http://stackoverflow.com/questions/5839794/printing-multiple-statements-in-cond-in-racket/5839851#5839851

-- 
  ((lambda (x) (x x)) (lambda (x) (x x)))  Eli Barzilay:
http://barzilay.org/   Maze is Life!
_
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users