-2

So basically this code's purpose is to simply print out the first n even numbers.

for (i = 0; i <=n; i+= 2)
{
    print i;
}

Thing is though, I don't understand Scheme at all. So, help please.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
Zeno
  • 138
  • 9
  • 3
    If you don't understand Scheme, learn it. Having people give you the Scheme equivalent is not going to help you much. – Bart Mar 17 '12 at 23:54
  • Nope, my homework is rewriting an algorithm in theta(n) time and presenting it in Scheme code. As you can see, I've already done the rewriting part ... just not in Scheme. – Zeno Mar 18 '12 at 05:07

3 Answers3

2

There are several ways to convert the code in the question to Scheme. The first one I can think of:

(define (print-even n)
  (let loop ((i 0))
    (if (<= i n)
        (begin
          (print i)
          (newline)
          (loop (+ i 2))))))

Notice this:

  • The solution is written as a recursive procedure
  • Instead of a for loop, I'm using a construct called a named let, which permits the initialization of some iteration variables (i in this case, initialized to 0) and the repeated execution of a recursive procedure (loop in this case), producing an effect similar to a for, even in performance
  • The stop condition in the "loop" is handled with essentially the same expression: repeat the body of the iteration as long as (<= i n), when that condition becomes false, the iteration ends
  • The begin surrounds the body of the "loop", just as the curly braces {} do in the original code
  • The print procedure performs the expected operation; for readability I added a new line after printing each number
  • The increment part of the original loop i += 2 is handled by the expression (+ i 2), inside the recursive call

So you see, the process being executed is essentially the same, only the way to write it (the syntax!) is different. Give it a try, type this:

(print-even 6)

... And the following will get printed on the screen:

0
2
4
6

Another possible way to implement the procedure, more similar to the original code, although (this is completely subjective) less idiomatic than the previous one:

(define (print-even n)
  (do ((i 0 (+ i 2))) ((> i n))
    (print i)
    (newline)))

Finally, if you're using Racket this will seem even more familiar to you:

#lang racket

(define (print-even n)
  (for ((i (in-range 0 (+ n 1) 2)))
    (print i)
    (newline)))
Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

The first big difference between Scheme and other languages is this: In Scheme, you do (almost) everything recursively.

To implement a simple loop, for instance, you would define a recursive function. This function would first check to see whether it's time to break out of the loop; if is is, it would return the final value. (There is no final value in this case, so it would just return something like (void) or '().) Otherwise, the function would do whatever it's supposed to do, then call itself again.

Any loop variables (such as i) become arguments to the function.

Hopefully this helps you understand how to do this.

Taymon
  • 24,950
  • 9
  • 62
  • 84
  • So something like: (define (calc-f-2 n) (* calc-f-2 (n)) 2))) would work? – Zeno Mar 18 '12 at 00:14
  • No, that would just recurse forever (and you're also missing an opening bracket). When calling the function recursively, you want to pass the _next_ value of the loop variable, after any changes in the loop; in this case, you'd do something like `(calc-f-2 (+ n 2))`. Also you need a conditional so it knows when to stop recursing, and you need to call an output function to actually do the printing. – Taymon Mar 18 '12 at 03:08
1

The Scheme way to do something like this is using a recursive function like the one below.

(define (doit value n)
  (if (<= value n)
    (begin
      ;;...perform loop body with x...
      (display value)(newline)
      (doit (+ value 2) n))))

To call this function you call (doit 2 n) where n is your n in the for loop.

With regards to learning Scheme, I recommend the first two links below.

For additional information on Scheme see

Community
  • 1
  • 1
Appleman1234
  • 15,946
  • 45
  • 67