I’d like to create sequences using the definitions below:
(define f1 (lambda (x) #t))
(define f2 (lambda (x) #f))
(define f3 (lambda (x) (if (null? x) #t (car x))))
(define f4 (lambda (x) (if (null? x) #t (not (car x)))))
My code is as follows:
(define (generate func n)
(let ((mylist '()))
(if (= n 0) mylist
(cons (func mylist) (generate func (- n 1)))
)))
It does the trick for f1 f2 and f3 but when i try f4 like (generate f4 10) it produces
(#t #t #t #t #t #t #t #t #t #t) instead of (#t #f #t #f #t #f #t #f #t #f).
Thanks for any help.
Well it happens because each time
funcis called with'()which is the value ofmylistinletscope. We should pass new list value to the next call ofgenerateto do something useful with it. Something like thisNow
(generate f4 4)produces'(#f #t #f #t)with first generated value coming last in the list. One mayreversethe result to get a “natural” order or useappendinstead ofconswhich would be less effective.