PasteRack.org
Paste # 82749
2018-08-09 07:35:49

Fork as a new paste.

Paste viewed 397 times.


Embed:

  1. #lang racket
  2.  
  3. (define (make-linear-producer x0 x1 a b)
  4.   (λ () (begin0 x0 (set!-values (x0 x1) (values x1 (+ (* a x0) (* b x1)))))))
  5.  
  6. (define-syntax (in-linear stx)
  7.   (syntax-case stx ()
  8.     [(_in-linear x0 x1 a b)
  9.      (syntax/loc stx (in-producer (make-linear-producer x0 x1 a b)))]))
  10.  
  11. ; Fibonacci
  12. (for/list ([n 10]
  13.            [x (in-linear 1 1 1 1)])
  14.   x)
  15.  
  16. ; Lucas
  17. (for/list ([n 10]
  18.            [x (in-linear 2 1 1 1)])
  19.   x)
  20.  

=>

'(1 1 2 3 5 8 13 21 34 55)

'(2 1 3 4 7 11 18 29 47 76)