For loop 球拍在一个数组中获得两个对象

For loop 球拍在一个数组中获得两个对象,for-loop,racket,For Loop,Racket,在我的作业中,我必须找出数组中的最大间隙 为了做到这一点,我得到了5个数字,并希望通过每一个数字,并检查它与下一个数字进行比较 我发现“for”是这样工作的: > (for/list ([i '(1 2 3)]) (* i i)) '(1 4 9) public void gettingSecondIndex(int a,int b,int c,int x,int y){ int[] abc = {a,b,c,x,y}; int index

在我的作业中,我必须找出数组中的最大间隙

为了做到这一点,我得到了5个数字,并希望通过每一个数字,并检查它与下一个数字进行比较

我发现“for”是这样工作的:

> (for/list ([i '(1 2 3)])
    (* i i))
'(1 4 9)
    public void gettingSecondIndex(int a,int b,int c,int x,int y){
        int[] abc = {a,b,c,x,y};
        int index, secondIndex;
        for(int i = 0 ; i < abc.length - 1 ; i++){
            index = abc[i]; // this is the index
            secondIndex = abc[i+1]; // this is what I am looking how to do in Racket
        }
    }
}
(define (getting-second-index lst)
  (for/list ([n1 lst]
             [n2 (rest lst)])
    (list n1 n2)))
问题是如何得到的不是‘i’位置,而是‘i’+1’位置。在java中,方式如下:

> (for/list ([i '(1 2 3)])
    (* i i))
'(1 4 9)
    public void gettingSecondIndex(int a,int b,int c,int x,int y){
        int[] abc = {a,b,c,x,y};
        int index, secondIndex;
        for(int i = 0 ; i < abc.length - 1 ; i++){
            index = abc[i]; // this is the index
            secondIndex = abc[i+1]; // this is what I am looking how to do in Racket
        }
    }
}
(define (getting-second-index lst)
  (for/list ([n1 lst]
             [n2 (rest lst)])
    (list n1 n2)))
public void gettingSecondIndex(inta、intb、intc、intx、inty){
int[]abc={a,b,c,x,y};
int索引,secondIndex;
对于(int i=0;i
处理Racket中的列表时,尽量忘记索引(它们不是数组!)。您可以通过以下方式获得所需的效果:

> (for/list ([i '(1 2 3)])
    (* i i))
'(1 4 9)
    public void gettingSecondIndex(int a,int b,int c,int x,int y){
        int[] abc = {a,b,c,x,y};
        int index, secondIndex;
        for(int i = 0 ; i < abc.length - 1 ; i++){
            index = abc[i]; // this is the index
            secondIndex = abc[i+1]; // this is what I am looking how to do in Racket
        }
    }
}
(define (getting-second-index lst)
  (for/list ([n1 lst]
             [n2 (rest lst)])
    (list n1 n2)))
现在您已经有了连续的对,继续按照您的意愿处理它们:

(getting-second-index '(1 2 3 4 5))
=> '((1 2) (2 3) (3 4) (4 5))
在处理Racket中的列表时,尽量忘记索引(它们不是数组!)。您可以通过以下方式获得所需的效果:

> (for/list ([i '(1 2 3)])
    (* i i))
'(1 4 9)
    public void gettingSecondIndex(int a,int b,int c,int x,int y){
        int[] abc = {a,b,c,x,y};
        int index, secondIndex;
        for(int i = 0 ; i < abc.length - 1 ; i++){
            index = abc[i]; // this is the index
            secondIndex = abc[i+1]; // this is what I am looking how to do in Racket
        }
    }
}
(define (getting-second-index lst)
  (for/list ([n1 lst]
             [n2 (rest lst)])
    (list n1 n2)))
现在您已经有了连续的对,继续按照您的意愿处理它们:

(getting-second-index '(1 2 3 4 5))
=> '((1 2) (2 3) (3 4) (4 5))

当最短的序列用完时,
理解将停止,因此
向右下拉
是不必要和浪费的。:-)嘿,谢谢你的回答!嗯,我真的是个新手,所以如果你能用这些行来解释我,这样我就能理解更多,我会很高兴:)@Tomer很简单:n1在列表上迭代,n2在同一个列表上迭代,没有第一个元素,然后循环体将一个列表中的一个元素与另一个列表中处于相同位置的元素配对。我们一直这样做,直到第二个列表的元素用完。当最短的序列用完时,
的理解将停止,因此,
向右下拉
是不必要和浪费的。:-)嘿,谢谢你的回答!嗯,我真的是个新手,所以如果你能用这些行来解释我,这样我就能理解更多,我会很高兴:)@Tomer很简单:n1在列表上迭代,n2在同一个列表上迭代,没有第一个元素,然后循环体将一个列表中的一个元素与另一个列表中处于相同位置的元素配对。我们这样做,直到第二个列表中的元素用完为止。