如何在球拍语言定索引处得到一个列表中的项目?(How do I get an item from a

2019-07-29 08:27发布

我想了一个循环语句定索引处获取某个列表中的项目。

(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
                      )))))])))

在这种情况下, best是列表,我需要在获得其价值countdown指数。 你能帮助我吗?

Answer 1:

例:

> (list-ref '(a b c d e f) 2)
'c

看到:

http://docs.racket-lang.org/reference/pairs.html



Answer 2:

或者这样建立自己:

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

但如果你想检查列表完成,不要被错误着急哟可以做到这一点,以及:

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


文章来源: How do I get an item from a list at a given index in racket language?