Lazy Lists (individual assignment)

(a) Write a pair of Scheme functions, (gen-list start stop) and (pair-sum? list val). The function gen-list will generate a list of consecutive integers, from start to stop. (If start > stop then the empty list is generated.) For example:

(gen-list 1 5) ---> (1 2 3 4 5)

The predicate pair-sum? tests whether any two adjacent values in numlist sum to val. For example,

(pair-sum? '(1 2 3) 3) ---> #t

since 1+2=3. Similarly,

(pair-sum? (gen-list 1 100) 1000) ---> #f

since no two adjacent integers in the range 1 to 100 can sum to 1000.

(b) An alternative to generating the entire list of numbers is to instead produce a lazy list. Consider the following example.

(define gen-lazy-list
  (lambda (start stop)
    (if (> start stop)
        #f
        (cons start
            (lambda () (gen-lazy-list (+ start 1) stop))))))

When called, gen-lazy-list defines a pair of values. The car is the first integer in the sequence. The cdr is a function that, when called, will return another pair. That pair consists of the next integer in the sequence plus another suspension function. When the end of the sequence is reached, the function in the cdr of the pair returns #f, indicating that no more values can be produced.

Rewrite your solution to pair-sum? as a new function called pair-sum-lazy? that takes a lazy integer sequence as defined by gen-lazy-list. pair-sum-lazy? should return either #t or #f.


You must use recursion, and not iteration. You may not use side-effects (e.g. set!).

Submit your code in a file called pairsum.rkt.