Skip to content

Instantly share code, notes, and snippets.

@cleoold
Created December 6, 2018 19:56
Show Gist options
  • Save cleoold/969cf2cc73658cf7b79c1062b9228e5f to your computer and use it in GitHub Desktop.
Save cleoold/969cf2cc73658cf7b79c1062b9228e5f to your computer and use it in GitHub Desktop.
Fibonacci sequence with offsets and weights
;; Fibonacci with offsets and weights
;; F(0) = A
;; F(1) = B
;; F(n) = xF(n-2) + yF(n-1)
(define (weighted-fibonacci n A B x y)
(cond
[(= n 0) A]
[(= n 1) B]
[else
(local
[(define (fib-adder a b counter)
(cond [(= n counter) (+ (* x a) (* y b))]
[else (fib-adder b (+ (* x a) (* y b)) (add1 counter))]))]
(fib-adder A B 2))]))
; (build-list 20
; (lambda (n) (weighted-fibonacci n 4 6 2 -1)))
; -> '(4 6 2 10 -6 26 -38 90 -166 346 -678 1370 -2726 5466 -10918 21850 -43686 87386 -174758 349530)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment