Created
September 11, 2022 08:31
-
-
Save kimtg/c786ffb2af5567b1c61aacdbc52a4482 to your computer and use it in GitHub Desktop.
memoized fibonacci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; memoized fibonacci | |
(define Fib (make-hash)) | |
(define (fib n) | |
(let ([v (hash-ref Fib n #f)]) | |
(cond [v v] | |
[else (let ([v (if (< n 2) n (+ (fib (- n 2)) (fib (- n 1))))]) | |
(hash-set! Fib n v) | |
v)]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment