8

How can I implement loop in plt-scheme like in java-

for(int i=0;i<10;){
     for(int j=0;j<3;){
          System.out.println(""+j);
          j++;
     }
      System.out.println(""+i);
      i++;
}
hello_there_andy
  • 2,039
  • 2
  • 21
  • 51
fireball003
  • 1,909
  • 5
  • 20
  • 24

4 Answers4

14

Your example in Java doesn't directly map onto the Scheme language by just learning a few new keywords as there aren't explicit constructs for implementing a for loop in Scheme (unless you write a construct yourself!). The cookbook way to do this in Scheme is to define a recursive function that loops over a list. Here's an example of how to do a for-loop style function in Scheme:

(define (doit x x-max dx)
  (if (<= x x-max)
    (begin
      ;;...perform loop body with x...
      (doit (+ x dx) x-max dx))))

(doit a b dx) ; execute loop from a to b in steps of dx

Taken from this page:

Guile and Scheme Links

Here's another link to a page that describes the ideas you need to understand to translate loops from imperative languages to Scheme:

Scheme Looping Constructs

Scheme is a really interesting language to learn, you should also read the Structure and Interpretation of Computer Programs, which is the textbook formerly used for teaching Scheme at MIT.

Svante
  • 50,694
  • 11
  • 78
  • 122
James Thompson
  • 46,512
  • 18
  • 65
  • 82
  • No problem, it makes me think fondly about my positive experiences with Scheme in college. Learning a functional language like Scheme can make you a better programmer, something about it helps you think about elegant solutions to problems. Once you feel like Scheme is starting to make sense, I recommend checking out another "different" language, like Prolog. That's different from both Scheme and Java, and it can teach you different new things. Good luck. :) – James Thompson Jun 12 '09 at 06:10
13

In PLT you can do this:

(for ([i (in-range 10)])
  (for ([j (in-range 3)]) (printf "~s\n" j))
  (printf "~s\n" i))
Eli Barzilay
  • 29,301
  • 3
  • 67
  • 110
8

The iteration construct in Scheme is "do", you can look it up in the R5RS specification.

The example you gave would look something like this:

(do ((i 0 (+ i 1))) ((> i 9))
  (do ((j 0 (+ j 1))) ((> j 2))
    (display j)
    (newline))
  (display i)
  (newline))

(do ...) is a bit more general than what shows up in this example. You can for example make it return a value instead of just using it for its side effects. It is also possible to have many "counters":

(do ((i 0 (+ i 1) 
     (j 0 (+ j 2)) 
    ((stop? i j) <return-value>)
   exprs...)
Jonas
  • 19,422
  • 10
  • 54
  • 67
  • 1
    to be clear, the second example with two loop variables will be tricky if you don't point out that all the variables are iterated at the same time -- one 'do' = one loop. this example would correspond closely to the C loop "for(i=0,j=0; !stop(i,j); i=i+1,j=j+2) {exprs();} return return_value;". Well, basically that's right. Mostly. – Aaron Jun 18 '09 at 06:09
  • Yes, that's true. I'll edit the post later and try to clear things up a bit. – Jonas Jun 18 '09 at 06:30
1

I'm suggesting you to take a look to Michele Simionato's "The adventures of a pythonista in schemeland". It's for python->scheme, but, it's really well written and, more importantly, it's from procedural->functional.

ZeD
  • 1,009
  • 6
  • 2