views:

181

answers:

1

Hi I want to return nothing when if condition is not met, for instance

(if x y z)

so if x is not met, z will be executed and I want to do nothing at z, just same as pythons "pass"

+8  A: 

From R5RS:

syntax: (if test consequent alternate)

syntax: (if test consequent)

Semantics: An if expression is evaluated as follows: first, test is evaluated. If it yields a true value (see section 6.3.1), then consequent is evaluated and its value(s) is(are) returned. Otherwise alternate is evaluated and its value(s) is(are) returned. If test yields a false value and no alternate is specified, then the result of the expression is unspecified.

So your if expression becomes:

(if x y)
eljenso
Note that one-branched if expressions are considered bad in PLT, and in fact they're not allowed in the default language. If you want it there, use `when` or `unless`.
Eli Barzilay
I actually tested it in PLT Scheme using R5RS language before posting, and it works without a problem.
eljenso
Yes, the r5rs language in PLT is pretty strict about following the report. Whether using one-sided `if`s is a good idea is a different question...
Eli Barzilay
In my experience, using `when` and `unless` is much better form for one-branch conditionals, as you don't need to wrap multi-statement bodies in a `(begin ...)`. If your scheme does not have them, they are trivial to write.
Brian Campbell
Listen to Eli : ) I had a long discussion with Eli about this and I have to agree that having a one sided if statement could be explosive.
kunjaan
It's because of undefined behaviour that the second type of if may not work. Undefined means implementation-defined, so to be safe, I would use `(if test consequent #f)`
omouse
@omouse What do you mean 'may not work'? It's not the *behaviour* that is undefined, it's the *result*.
eljenso