-6

What's the equivalence in scheme for the following code?

if (condition)
{
  function1;
  function2;
}
else
{
  function3;
  function4;
}

Thanks.

uSOf
  • 19
  • 2
  • 3
    This question appears to be off-topic because it is about a basic programming control construct (if/then/else). – Joshua Taylor Nov 24 '13 at 20:44
  • It is very basic knowledge, there are tons of resources about it. Feel free to read SICP it should answer most of your questions. – Loïc Faure-Lacroix Nov 27 '13 at 14:59
  • possible duplicate of [How does \`if\` statement work in Scheme?](http://stackoverflow.com/questions/5751052/how-does-if-statement-work-in-scheme) – Johann Blais Apr 01 '14 at 07:36

5 Answers5

4

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)))
ben rudgers
  • 3,647
  • 2
  • 20
  • 32
3

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
Sylwester
  • 47,942
  • 4
  • 47
  • 79
1

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.

daniel gratzer
  • 52,833
  • 11
  • 94
  • 134
0

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))
Krease
  • 15,805
  • 8
  • 54
  • 86
0

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

Community
  • 1
  • 1
uSOf
  • 19
  • 2