user5386463
user5386463

Reputation: 43

Racket - How to count the number of elements in a list?

I was wondering how to count the number of elements For example, counting the number of elements in (list 'a 'b 'c' 'd). Thank you!

Upvotes: 1

Views: 13799

Answers (2)

AleArk
AleArk

Reputation: 417

If you want to do see how you can do it yourself recursively, one way to do it is this:

(define (my-length lst)
  (if (empty? lst)
      0
      (+ 1 (my-length (rest lst)))))

Upvotes: 1

Halvra
Halvra

Reputation: 295

Based on racket documentation :

http://docs.racket-lang.org/reference/pairs.html#%28def.%28%28quote.~23~25kernel%29._length%29%29

(length lst) Returns the number of elements in lst.

Upvotes: 6

Related Questions