boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

004-alist.scm (1235B)


      1 ;; tests/cc-util/04-alist.scm — alist-ref / alist-ref/eq / alist-set /
      2 ;; alist-update.
      3 ;; Assumes cc/util.scm is loaded.
      4 
      5 ;; alist-ref uses equal? — bv keys
      6 (define al1 (alist-set "k1" 1 (alist-set "k2" 2 '())))
      7 (if (= (alist-ref "k1" al1) 1)      0 (sys-exit 1))
      8 (if (= (alist-ref "k2" al1) 2)      0 (sys-exit 2))
      9 (if (alist-ref "k3" al1)            (sys-exit 3) 0)   ; missing -> #f
     10 
     11 ;; alist-set conses on the front (later entries shadow older ones via search order)
     12 (define al2 (alist-set "k1" 99 al1))
     13 (if (= (alist-ref "k1" al2) 99)     0 (sys-exit 4))
     14 
     15 ;; alist-ref/eq uses eq? — symbol keys
     16 (define al3 (alist-set 'foo 10 (alist-set 'bar 20 '())))
     17 (if (= (alist-ref/eq 'foo al3) 10)  0 (sys-exit 5))
     18 (if (= (alist-ref/eq 'bar al3) 20)  0 (sys-exit 6))
     19 (if (alist-ref/eq 'baz al3)         (sys-exit 7) 0)
     20 
     21 ;; alist-update: present case replaces value
     22 (define al4 (alist-update "k1" (lambda (v) (+ v 100)) al1))
     23 (if (= (alist-ref "k1" al4) 101)    0 (sys-exit 8))
     24 (if (= (alist-ref "k2" al4) 2)      0 (sys-exit 9))   ; other key untouched
     25 
     26 ;; alist-update: missing key -> upsert with (f #f)
     27 (define al5 (alist-update "new" (lambda (v) (if v v 7)) '()))
     28 (if (= (alist-ref "new" al5) 7)     0 (sys-exit 10))
     29 
     30 (sys-exit 0)