What's the equivalence in scheme for the following code?
if (condition)
{
function1;
function2;
}
else
{
function3;
function4;
}
Thanks.
What's the equivalence in scheme for the following code?
if (condition)
{
function1;
function2;
}
else
{
function3;
function4;
}
Thanks.
The parallel code where the code executes a block for each of the true and false branches is:
(if (condition)
(begin
(function1)
(function2))
(begin
(function3)
(function4)))
It depends. You really should try to narrow you question further with an actual problem in scheme since the answer will depend on what you are trying to do.
In idiomatic Scheme most should be done without side effects so you have
(if predicate-expression
consequent-expression
alternative-expression) ;; alternative is optional but should be used anyway.
But with your code you have more than one thing in the branches, something like:
int test (condition)
{
if (condition)
{
function1();
return function2();
}
else
{
function3();
return function4();
}
}
Here, in order for function1
and function3
to do anything useful it has to mutate, read or write something, also called a side effect. You should try to avoid that so in Scheme you either use a let
to store a temporary variable, do the call so when it returns it is used like this:
(define (test condition)
(if condition
(let ((tmp (function3)))
(function4 tmp))
(function4 (function3))))
Now. You will eventually need side effects for a few procedures and then you need to use begin
or use cond
which has explicit begin.
(if predicate-expression
(begin
consequent-side-effect-expression
consequent-tail-expression)
(begin
alternative-side-effect-expression
alternative-tail-expression))
Now begin
joins together several expressions into one and the result of a begin block is it's last expression, thus you can even use it in the predicate. Cond has an explicit begin in every consequent so I often switch from if
to cond
when I either need more than one consequent or a begin:
(cond (predicate-expression consequent-side-effect-expression
consequent-tail-expression)
(predicate2-expression consequent2-tail-expression2)
(else alternative-side-effect-expression
alternative-tail-expression))
Often when there is side effects you not always need the alternative. Eg. you are actually not interested in what the if
returns because it may not be in tail position either. In these cases you have in the (rnrs control (6))
library when
and unless
with explicit begin:
(when expression
consequent-side-effect-expression
...
consequent-tail-expression)
(unless expression
consequent-side-effect-expression
...
consequent-tail-expression)
A let
or a procedure
also has explicit begin:
(let ()
(display "hello") ; displays "hello"
5) ; returns 5
(define (test x)
(display x) ; display x
(+ x 5)) ; return x+5
How about
(if condition
then-code
else-code)
Scheme also has the more general
(cond
((test1) case-1)
((test2) case-2)
...
(else else-case))
If you are using Racket, then
(when test then-code)
is if
without the else.
There are a few Intro to Scheme sites that cover this sort of thing. Here's an example from the link included:
(define (min a b)
(if (< a b)
a
b))
WorBlux replied in another thread:
(if condition1 (begin function1 function2) (begin function3 function4)) 'begin' is a procedure/macro that forces sequential evaluation of each of its arguments from left to right, and returns the value of the last argument evaluated. In the cond form each clause is wrapped in an implicit begin. Also the define special form is also so wrapped, so you can do (cond (condition1 function1 function2) (else function3 function4)) – WorBlux