I am reading a programming languages book and it is asking me to to explain what the following Scheme function does (not sure, can someone help explain):
(define (x lis)
(cond ((null? lis) 0)
((not (list? (car lis)))
(cond
((eq? (car lis) #f) (x (cdr lis)))
(else (+ 1 (x (cdr lis))))))
(else (+ (x (car lis)) (x (cdr lis))))))
It counts the number of leaf nodes a nested list structure, ignoring #f
. It uses a recursive procedure:
car
is not a list:
car
is #f
, we recurse on the cdr
and return that. car
as 1, and add that to the result of recursing on the cdr
. car
and cdr
, and add them together.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.