kahl

R⁷RS Scheme parser combinator library for decoding BARE messages

git clone https://git.8pit.net/kahl.git

 1(define (inc n) (+ n 1))
 2(define (dec n) (- n 1))
 3
 4(define msb 7) ;; most-significant bit
 5(define lsb 0) ;; least-significant bit
 6
 7(define (lsb-set? i) (bit-set? lsb i))
 8(define (msb-set? i) (bit-set? msb i))
 9
10(define (range upto)
11  (if (zero? upto)
12    '()
13    (let ((i (dec upto)))
14      (append (range i) (list i)))))
15
16(define (bytevector-fold-right proc seed bv)
17  (define (%bytevector-fold-right n)
18    (if (>= n (bytevector-length bv))
19      seed
20      (proc (bytevector-u8-ref bv n)
21            (%bytevector-fold-right (inc n)))))
22
23  (if (zero? (bytevector-length bv))
24    seed
25    (%bytevector-fold-right 0)))
26
27(define (lset-unique? lset)
28  (call-with-current-continuation
29    (lambda (k)
30      (letrec ((proc (lambda (l)
31                       (if (null? l)
32                         #t
33                         (if (memv (car l) (cdr l))
34                           (k #f)
35                           (proc (cdr l)))))))
36        (k (proc lset))))))
37
38;; Like init from Haskell, returns everything except the last list element.
39
40(define (init lst)
41  (define (%init lst)
42    (if (eqv? 1 (length lst))
43      '()
44      (cons (car lst) (%init (cdr lst)))))
45
46  (if (null? lst)
47    (error "empty list")
48    (%init lst)))