Scheme 中的深度递归
Deep recursion in Scheme
我对方案中的深度递归有疑问
输出应该是
(1 2 3 (4 5))
~> ((1) (2) (3) ((4) (5)))
但我的输出是(1 (2 (3 ((4 (5 ())) ()))))
。
好像引文放错地方了
我得到了这些
(define (DoublebubbleLst lst)
(cond ((null? lst) lst)
((not (pair? lst))
(append lst))
(else(list
(DoublebubbleLst (car lst))
(DoublebubbleLst (cdr lst))))))
我的问题是什么
这似乎有效:
(define (double-bubble-list lst)
(cond ((null? lst) lst)
((not (list? (car lst)))
(cons (list (car lst))
(double-bubble-list (cdr lst))))
(else (cons (double-bubble-list (car lst))
(double-bubble-list (cdr lst))))))
示例:
> (double-bubble-list '(1 2 3 (4 5)))
'((1) (2) (3) ((4) (5)))
我对方案中的深度递归有疑问 输出应该是
(1 2 3 (4 5))
~> ((1) (2) (3) ((4) (5)))
但我的输出是(1 (2 (3 ((4 (5 ())) ()))))
。
好像引文放错地方了
我得到了这些
(define (DoublebubbleLst lst)
(cond ((null? lst) lst)
((not (pair? lst))
(append lst))
(else(list
(DoublebubbleLst (car lst))
(DoublebubbleLst (cdr lst))))))
我的问题是什么
这似乎有效:
(define (double-bubble-list lst)
(cond ((null? lst) lst)
((not (list? (car lst)))
(cons (list (car lst))
(double-bubble-list (cdr lst))))
(else (cons (double-bubble-list (car lst))
(double-bubble-list (cdr lst))))))
示例:
> (double-bubble-list '(1 2 3 (4 5)))
'((1) (2) (3) ((4) (5)))