here way of building fibonacci sequence list values not exceed x:
(define (fibs-upto x) (for/list ([i (in-naturals)] #:break (> (fib i) x)) (fib i))) is there another, maybe cleaner way of doing without using #:break, , without using #lang lazy build infinite lazy list?
here solution in (fib i) evaluated once.
(define (fibs-upto x) (for*/list ([i (in-naturals)] [fib-i (in-value (fib i))] #:break (> fib-i x)) fib-i)) but might easier read standard loop:
(define (fibs-upto x) (define (loop i) (define fib-i (fib i)) (if (> fib-i x) '() (cons fib-i (loop (+ 1))))) (loop 0)) that said, important fib cache computed values above solutions o(n).
update
a version using sequence-map:
(define (fibs-upto x) (for/list ([y (sequence-map fib (in-naturals))] #:break (> y x)) y))
Comments
Post a Comment