问题描述
sash> (repli '(a b c) 3)
sash> (a a a b b b c c c)
解法
(define repli
(lambda (ls n)
(let f ([s ls]
[m n])
(if (null? s)
'()
(if (zero? m) (f (cdr s) n) (let ([e (car s)]) (cons e (f s (- m 1)))))))))
与上一个P14类似,也可将每个元素重复形成相应的子列表,然后用append
拼接起来。