Last active
May 23, 2018 11:30
-
-
Save skalahonza/fe1d2234bc7c25a8e361ba60660bc452 to your computer and use it in GitHub Desktop.
Trivia map implementation in scheme (really stupid and slow)
This file contains hidden or 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
(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