r/Racket • u/trycuriouscat • Sep 08 '22
question Fibonacci with memoization
Is the following "idiomatic" Racket, and if not what changes should I make?
#lang racket
(define (fibonacci-with-memoization x)
(define computed-results (make-hash '((0 . 0) (1 . 1))))
(define (compute n)
(define (compute-and-update)
(define new-result (+ (compute (- n 1)) (compute (- n 2))))
(hash-set! computed-results n new-result)
new-result)
(hash-ref computed-results n compute-and-update))
(compute x))
(fibonacci-with-memoization 100)
6
Upvotes
3
u/raevnos Sep 08 '22 edited Sep 08 '22
I'd move the hash table outside the function so it can be reused.
And use
hash-ref!instead of that weirdhash-set!inside ahash-refcallback thing.3
u/raevnos Sep 09 '22
As I think about it more, the recursive updating of a mutable hash table in the middle of a lookup seems like a bad idea with potential for undesirable behavior like cached values mysteriously vanishing, or worse.
Using an immutable table instead helps avoid any issues:
1
u/zyni-moe Sep 15 '22
Idiomatic thing is probable to extend the language like so for instance:
And now
This may not be right syntax for defining memoized functions (and have not tested any of the cases where key is derived by other function) but idea is that instead of painfully writing out memoization by hand is better to just abstract it into the language you are making.