I have a lazy list interface and I am trying to use it to build a lazy list of subsets given a list.
The interface:
(define cons-lzl cons)
(define empty-lzl empty)
(define empty-lzl? empty?)
(define head car)
(define tail
(lambda (lz-lst)
((cdr lz-lst))))
The goal is to have a lazy list generated by the function and using take function to obtain a given number of the subsets if wanted.
My take function:
(define take
(lambda (lz-lst n)
(if (or (= n 0) (empty-lzl? lz-lst))
empty-lzl
(cons (head lz-lst)
(take (tail lz-lst) (- n 1))))))
Test case:
(take (all-subs '(1 2 3)) 8) ->
'(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))
The way I see it I have several steps:
- Check if list empty - if so return a constructed empty lazy list.
- If the list isn't empty, return a lazy list with the car element and call all-subs again with the cdr element.
How can I proceed?