Skip to content

Instantly share code, notes, and snippets.

@sjqtentacles
Created July 17, 2021 04:50
Show Gist options
  • Save sjqtentacles/742aebc6c8072222010768575520f370 to your computer and use it in GitHub Desktop.
Save sjqtentacles/742aebc6c8072222010768575520f370 to your computer and use it in GitHub Desktop.
biwascheme utilities for python-style dictionaries, plus a few other handy functions
(define (zip lis1 lis2)
(if (and (not (equal? lis1 '())) (not (equal? lis2 '())))
(cons (cons (car lis1) (car lis2)) (zip (cdr lis1) (cdr lis2)))
'()))
(define (take ls count)
(if (and (> count 0) (not (equal? ls '())))
(cons (car ls) (take (cdr ls) (- count 1)))
'()))
(define (drop ls count)
(cond
((<= count 0) ls)
((>= count (length ls)) '())
(else (drop (cdr ls) (- count 1)))))
(define (last ls)
(car (reverse ls)))
(define (is-even? num)
(equal? (mod num 2) 0))
(define (is-odd? num)
(equal? (mod num 2) 1))
(define (split-list ls)
(let ((with-nums (zip (iota (length ls)) ls)))
(list
(map cdr (filter (lambda (x) (is-even? (car x))) with-nums))
(map cdr (filter (lambda (x) (is-odd? (car x))) with-nums)))))
(define-macro (dict keyvals)
(let ((temp-table (gensym))
(temp-pair (gensym)))
`(let ((,temp-table (make-eq-hashtable)))
(for-each (lambda (,temp-pair)
(hashtable-set! ,temp-table (car ,temp-pair) (cadr ,temp-pair)))
',keyvals)
,temp-table)))
(define-macro (get-dict table key)
`(hashtable-ref ,table ',key ()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment