3 (provide blank-exn? make-blank-exn mal-exn? make-mal-exn mal-exn-val
4 malfunc malfunc? malfunc-fn
5 malfunc-ast malfunc-env malfunc-params malfunc-macro? malfunc-meta
6 _partition _equal? _printf
7 nil _nil? _keyword _keyword?
8 _to_list _sequential? _count _empty? _nth _first _rest _map
10 atom atom? atom-val set-atom-val!)
12 (define-struct (blank-exn exn:fail:user) ())
13 (define-struct (mal-exn exn:fail:user) [val])
19 (define nil (new nil%))
24 (struct malfunc [fn ast env params macro? meta]
25 #:property prop:procedure (struct-field-index fn))
29 ;; From: http://stackoverflow.com/questions/8725832/how-to-split-list-into-evenly-sized-chunks-in-racket-scheme/8731622#8731622
30 (define (_partition n xs)
33 (let ((first-chunk (take xs n))
35 (cons first-chunk (_partition n rest)))))
37 (define (_equal_seqs? seq_a seq_b)
38 (let ([a (_to_list seq_a)]
40 (and (= (length a) (length b))
41 (andmap (lambda (va vb) (_equal? va vb)) a b))))
43 (define (_equal_hashes? a b)
44 (if (= (hash-count a) (hash-count b))
45 (let ([keys (hash-keys a)])
46 (andmap (lambda (k) (_equal? (_get a k) (_get b k))) keys))
51 [(and (_sequential? a) (_sequential? b)) (_equal_seqs? a b)]
52 [(and (hash? a) (hash? b)) (_equal_hashes? a b)]
56 (define _printf (lambda a (apply printf a) (flush-output)))
59 (define (_keyword str)
60 (string-append "\u029e" str))
63 (and (string? k) (regexp-match? #px"^\u029e" k)))
69 (if (vector? a) (vector->list a) a))
71 (define (_sequential? seq)
72 (or (vector? seq) (list? seq)))
76 [(vector? seq) (vector-length seq)]
82 (define (_nth seq idx)
83 (cond [(>= idx (_count seq)) (raise "nth: index out of range")]
84 [(vector? seq) (vector-ref seq idx)]
85 [else (list-ref seq idx)]))
88 (cond [(vector? seq) (if (_empty? seq) nil (vector-ref seq 0))]
89 [else (if (_empty? seq) nil (list-ref seq 0))]))
92 (cond [(vector? seq) (if (_empty? seq) '() (rest (vector->list seq)))]
93 [else (if (_empty? seq) '() (rest seq))]))
96 (cond [(vector? seq) (vector-map f seq)]
102 (let ([new-hm (hash-copy (first args))]
103 [pairs (_partition 2 (rest args))])
105 (hash-set! new-hm (first k_v) (second k_v))) pairs)
110 (let ([new-hm (hash-copy (first args))])
111 (map (lambda (k) (hash-remove! new-hm k)) (rest args))
115 (cond [(_nil? hm) nil]
116 [(dict-has-key? hm k) (hash-ref hm k)]
120 (struct atom [val] #:mutable)