Skip to content

Instantly share code, notes, and snippets.

@skalahonza
Last active May 23, 2018 11:30
Show Gist options
  • Save skalahonza/fe1d2234bc7c25a8e361ba60660bc452 to your computer and use it in GitHub Desktop.
Save skalahonza/fe1d2234bc7c25a8e361ba60660bc452 to your computer and use it in GitHub Desktop.
Trivia map implementation in scheme (really stupid and slow)
(define first car)
(define second cadr)
(define rest cdr)
(define (third list) (car (cddr list)))
(define fourth cadddr)
(define initmap '())
(define test
(list (list 'a 1) (list 'b 3))
)
(define (lookup map key default)
(cond
((eq? '() map) default)
((eq? (first (first map)) key) (second (first map)))
(#t (lookup (rest map) key default))
)
)
(define (add map key val)
(cons (list key val) (delete map key))
)
(define (delete map key)
(delete-help map key '())
)
(define (delete-help map key before)
(cond
((eq? map '()) before)
((eq? (first (first map)) key) (append before (rest map))) ;key to delete found
(#t (delete-help (rest map) key (cons (first map) before))) ;this is not meant to be deleted
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment