Question

I would like to find how many lists exist in a list in Scheme.

for example:

> (numberOfLists 'a) --> 0

> (numberOfLists '(1 2 3)) --> 0

> (numberOfLists '(1 (2 3) asf)) --> 1

> (numberOfLists '(s1 (1 2 3) (4 5) (6 7))) --> 3

Thank you for your help in advance.

Was it helpful?

Solution

Uses SRFI 1:

(define (number-of-lists lst)
  (if (list? lst)
      (count list? lst)
      0))

OTHER TIPS

If your Scheme implementation provides count, you can use the answer by Chris Jester-Young. If not, you can implement number-of-lists as:

(define (number-of-lists lst)

   (define (helper in count)
      (if (null? in)
        count
        (helper (cdr in) (if (list? (car in)) (+ 1 count) count))))

   (if (list? lst)
     (helper lst 0)
     0))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top