1

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:

  1. Check if list empty - if so return a constructed empty lazy list.
  2. 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?

Adam Morad
  • 167
  • 1
  • 7
  • 1
    Your `cons-lzl` won't work. It needs to be syntax. Where is `all-subs`? – Sylwester Jun 18 '18 at 17:24
  • 2
    see the correct `take` [here](http://rosettacode.org/wiki/Sieve_of_Eratosthenes#SICP-style_streams). yours makes a classic mistake of forcing one too many tails, which can even lead to divergence, quite unnecessarily. see SRFI-40, SRFI-41, SRFI-45. – Will Ness Jun 19 '18 at 09:00
  • to your question, I should imagine just using the same algorithm as [in strict Scheme](https://stackoverflow.com/questions/20622945/how-to-do-a-powerset-in-drracket) should work without modification (except for replacing each `cons` with the lazy cons, of course). So this might even be a duplicate. – Will Ness Jun 20 '18 at 15:56

0 Answers0