문제

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.

도움이 되었습니까?

해결책

Uses SRFI 1:

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

다른 팁

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))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top