Skip to content

Instantly share code, notes, and snippets.

@NalaGinrut
Created January 14, 2017 14:30
Show Gist options
  • Save NalaGinrut/ca8e6c094bb4b82c94cb46f37ea891a0 to your computer and use it in GitHub Desktop.
Save NalaGinrut/ca8e6c094bb4b82c94cb46f37ea891a0 to your computer and use it in GitHub Desktop.
Simple LRU implementation in Scheme
(define (make-lru size)
(define cnt 0)
(define fl '())
(define ht (make-hash-table))
(define (refresh-fl i)
(set! fl (cons i (filter (lambda (x) (not (= x i))) fl))))
(lambda (cmd . i/v)
(case cmd
((get)
(refresh-fl (car i/v))
(hash-ref ht (car i/v) "no"))
((set)
(display fl) (newline)
(if (= cnt size)
(let ((i (list-ref fl (1- cnt))))
(hash-set! ht i (car i/v))
(refresh-fl i))
(begin
(set! fl (cons cnt fl))
(hash-set! ht cnt (car i/v))
(set! cnt (1+ cnt))))
(hash-map->list cons ht))
((cnt) cnt)
((fl) fl))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment