-
Notifications
You must be signed in to change notification settings - Fork 3
/
alists.sls
59 lines (49 loc) · 1.85 KB
/
alists.sls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!r6rs
;;; alists.sls --- Alist Utilities
;; Copyright (C) 2014 Ian Price <[email protected]>
;; Author: Ian Price <[email protected]>
;; This program is free software, you can redistribute it and/or
;; modify it under the terms of the new-style BSD license.
;; You should have received a copy of the BSD license along with this
;; program. If not, see <http://www.debian.org/misc/bsd.license>.
(library (pfds private alists)
(export alist-ref
alist-set
alist-delete
alist-update
)
(import (rnrs base)
(only (srfi :1 lists) assoc)
)
(define (alist-ref alist key default eqv?)
(cond ((assoc key alist eqv?) => cdr)
(else default)))
(define (alist-set alist key value eqv?)
;; TODO: measure to see if it is even worth it
;; adds key value to alist if key is not in alist
;; if key is in a list, replaces the association
;; does not preserve order.
(let loop ((new '()) (old alist))
(cond ((null? old)
(cons (cons key value) new))
((eqv? (car (car old)) key)
(cons (cons key value)
(append (cdr old) new)))
(else (loop (cons (car old) new) (cdr old))))))
;;((al (alist-delete (collision-alist node) key eqv?)))
(define (alist-delete alist key eqv?)
;; TODO: measure to see if it is even worth it
(let loop ((new '()) (old alist))
(cond ((null? old) new)
((eqv? (car (car old)) key)
(append (cdr old) new))
(else (loop (cons (car old) new) (cdr old))))))
(define (alist-update alist key update base eqv?)
(let loop ((new '()) (old alist))
(cond ((null? old)
(cons (cons key (update base)) new))
((eqv? (car (car old)) key)
(cons (cons key (update (cdr (car old))))
(append (cdr old) new)))
(else (loop (cons (car old) new) (cdr old))))))
)