list 如何从球拍语言中给定索引处的列表中获取项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10523361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:50:31  来源:igfitidea点击:

How do I get an item from a list at a given index in racket language?

listschemeracket

提问by lu1s

I'm trying to get an item from a list at a given index for a loop statement.

我正在尝试从循环语句的给定索引处的列表中获取项目。

(define decision-tree-learning
  (lambda (examples attribs default)
    (cond
      [(empty? examples) default]
      [(same-classification? examples) (caar examples)] ; returns the classification
      [else (lambda () 
              (let ((best (choose-attribute attributes examples))
                    (tree (make-tree best))
                    (m (majority-value examples))
                    (i 0)
                    (countdown (length best)) ; starts at lengths and will decrease by 1
                  (let loop()
                    (let example-sub ; here, totally stuck now
                      ; more stuff
                      (set! countdown (- countdown 1))
                      ; more stuff
                      )))))])))

In this case, bestis the list and I need to get its value at the countdownindex. Could you help me on that?

在这种情况下,best是列表,我需要在countdown索引处获取它的值。你能帮我吗?

回答by soegaard

回答by Gil Matzov

Or build this yourself:

或者自己构建:

(define my-list-ref
    (lambda (lst place)
      (if (= place 0)
          (car lst)
          (my-list-ref (cdr lst) (- place 1)))))

but if you want to check if the list is done and don't worry by error yo can do this as well:

但是如果你想检查列表是否完成并且不要担心错误,你也可以这样做:

(define my-list-ref
    (lambda (lst place)
      (if (null? lst)
          '()
          (if (= place 0)
          (car lst)
          (my-list-ref (cdr lst) (- place 1))))))