Loop in PLT Scheme

前端 未结 4 805
半阙折子戏
半阙折子戏 2020-12-30 11:31

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++;
         


        
4条回答
  •  情话喂你
    2020-12-30 11:54

    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) )
       exprs...)
    

提交回复
热议问题