15

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

(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索引处获取它的值。你能帮我解决这个问题吗?

4

2 回答 2

31

例子:

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

看:

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

于 2012-05-09T20:20:12.150 回答
7

或者自己构建:

(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))))))
于 2012-05-15T15:23:48.670 回答