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

青春壹個敷衍的年華 提交于 2019-12-04 02:31:08

Example:

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

See:

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

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))))))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!