i want to implement a lazy list that get 2 numbers as argument: low & int. The lambda will returns the lazy-list of all integers that are larger than low and divisible by int.
For example:
> (define lz1 (div-from 5 12))
> (take lz1 3)
'(12 24 36)
My try of implementation:
>(define gen_item
(lambda (n int)
(cons (cond ((= 0 (modulo n int)) n))
(lambda () (gen_item (+ n 1) int)))))
When using this take implemenatation:
>(define take
(lambda (lz-lst n)
(if (= n 0)
(list)
(cons (car lz-lst)
(take (tail lz-lst) (sub1 n))))))
When i run the lambda:
(take (gen_item 5 12) 20)
The return value:
'(#<void> #<void> #<void> #<void> #<void>
#<void> #<void> 12 #<void> #<void>
#<void> #<void> #<void> #<void> #<void>
#<void> #<void> #<void> #<void> 24)
How can i prevent the lambda return #<void>
and return nothing instead?
Thank you.
your cond
does not have a default case. So when (modulo n int)
is not zero your get a undefined value. Add one like this:
(cond (predicat-expression consequent-expression)
(predicat-expression2 consequent-expression2)
(else alternative-expression))
Since you only have one consequent expression you could do this with if
(if predicate-expression
consequent-expression
alternative-expression)
Now if you don't any elements when predicate is not true then yuu should not cons anything when it's false but continue to the next true value:
(define gen_item
(lambda (n int)
(if (= 0 (modulo n int))
(cons n (lambda () (gen_item (+ n 1) int)))
(gen_item (+ n 1) int))))