How to get an item from a list at a specified index in racket language?

I am trying to get an item from a list at the specified 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, best is a list, and I need to get its value in the countdown index. could you help me?

+8
list scheme racket
source share
2 answers

Example:

 > (list-ref '(abcdef) 2) 'c 

Cm:

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

+18
source share

Or create it 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 complete and not worried by mistake, you can also do this:

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

All Articles