using stdint types
This commit is contained in:
+118
-114
@@ -5,13 +5,17 @@
|
||||
|
||||
;; (kk)
|
||||
|
||||
(define pair cons)
|
||||
(define first car)
|
||||
(define rest cdr)
|
||||
|
||||
(define hm/set! hash-map-set!)
|
||||
(define hm/get hash-map-get)
|
||||
|
||||
(define (hm/get-or-nil hm key)
|
||||
(mytry (hm/get hm key) ()))
|
||||
(attempt (hm/get hm key) ()))
|
||||
|
||||
(define-syntax (pe expr)
|
||||
(define-macro (pe expr)
|
||||
`(begin
|
||||
(print :end " " ',expr "evaluates to")
|
||||
((lambda (e)
|
||||
@@ -23,16 +27,16 @@
|
||||
|
||||
(define (stream-null? s) (when s t))
|
||||
|
||||
(define-syntax (delay expr)
|
||||
(define-macro (delay expr)
|
||||
`(,lambda () ,expr))
|
||||
|
||||
(define (force promise)
|
||||
(promise))
|
||||
|
||||
(define-syntax (mac a) (list + 1 1))
|
||||
(define-syntax (add . args) (pair '+ args))
|
||||
(define-macro (mac a) (list + 1 1))
|
||||
(define-macro (add . args) (cons '+ args))
|
||||
|
||||
(define-syntax (and . args)
|
||||
(define-macro (and . args)
|
||||
;; (and cond1 cond2 (cond3 args))
|
||||
;; ->
|
||||
;; (if cond1
|
||||
@@ -44,12 +48,12 @@
|
||||
;; ())
|
||||
;; ())
|
||||
(if args
|
||||
`(,if ,(first args)
|
||||
,(apply and (rest args))
|
||||
`(,if ,(car args)
|
||||
,(apply and (cdr args))
|
||||
())
|
||||
t))
|
||||
|
||||
(define-syntax (or . args)
|
||||
(define-macro (or . args)
|
||||
;; (or cond1 cond2 (cond3 args))
|
||||
;; ->
|
||||
;; (if cond1
|
||||
@@ -60,12 +64,12 @@
|
||||
;; t
|
||||
;; ())))
|
||||
(if args
|
||||
`(,if ,(first args)
|
||||
`(,if ,(car args)
|
||||
t
|
||||
,(apply or (rest args)))
|
||||
,(apply or (cdr args)))
|
||||
()))
|
||||
|
||||
(define-syntax (when condition . body)
|
||||
(define-macro (when condition . body)
|
||||
"Special form for when multiple actions should be done if a
|
||||
condition is true.
|
||||
|
||||
@@ -80,68 +84,68 @@ condition is true.
|
||||
(print \"World!\"))
|
||||
{{{example_end}}}
|
||||
"
|
||||
(if (= (rest body) ())
|
||||
(if (= (cdr body) ())
|
||||
`(if ,condition ,@body nil)
|
||||
`(if ,condition (begin ,@body) nil)))
|
||||
|
||||
|
||||
(define-syntax (unless condition . body)
|
||||
(define-macro (unless condition . body)
|
||||
"Special form for when multiple actions should be done if a
|
||||
condition is false."
|
||||
(if (= (rest body) ())
|
||||
(if (= (cdr body) ())
|
||||
`(if ,condition nil ,@body)
|
||||
`(if ,condition nil (begin ,@body))))
|
||||
|
||||
(define-syntax (n-times times action)
|
||||
(define-macro (n-times times action)
|
||||
"Executes action times times."
|
||||
(define (repeat times elem)
|
||||
(unless (> 1 times)
|
||||
(pair elem (repeat (- times 1) elem))))
|
||||
(cons elem (repeat (- times 1) elem))))
|
||||
`(begin ,@(repeat times action)))
|
||||
|
||||
(define-syntax (let bindings . body)
|
||||
(define-macro (let bindings . body)
|
||||
(define (unzip lists)
|
||||
(when lists
|
||||
(define (iter lists l1 l2)
|
||||
(define elem (first lists))
|
||||
(define elem (car lists))
|
||||
(if elem
|
||||
(iter (rest lists)
|
||||
(pair (first elem) l1)
|
||||
(pair (first (rest elem)) l2))
|
||||
(iter (cdr lists)
|
||||
(cons (car elem) l1)
|
||||
(cons (car (cdr elem)) l2))
|
||||
(list l1 l2)))
|
||||
(iter lists () ())))
|
||||
(define unzipped (unzip bindings))
|
||||
`((,lambda ,(first unzipped) ,@body) ,@(first (rest unzipped))))
|
||||
`((,lambda ,(car unzipped) ,@body) ,@(car (cdr unzipped))))
|
||||
|
||||
(define-syntax (cond . clauses)
|
||||
(define-macro (cond . clauses)
|
||||
(define (rec clauses)
|
||||
(if (= () clauses)
|
||||
()
|
||||
(if (= (first (first clauses)) 'else)
|
||||
(if (= (car (car clauses)) 'else)
|
||||
(begin
|
||||
(if (not (= (rest clauses) ()))
|
||||
(if (not (= (cdr clauses) ()))
|
||||
(error :syntax-error "There are additional clauses after the else clause!")
|
||||
(pair 'begin (rest (first clauses)))))
|
||||
`(if ,(first (first clauses))
|
||||
(begin ,@(rest (first clauses)))
|
||||
,(rec (rest clauses))))))
|
||||
(cons 'begin (cdr (car clauses)))))
|
||||
`(if ,(car (car clauses))
|
||||
(begin ,@(cdr (car clauses)))
|
||||
,(rec (cdr clauses))))))
|
||||
(rec clauses))
|
||||
|
||||
(define-syntax (case var . clauses)
|
||||
(define-macro (case var . clauses)
|
||||
(define (rec clauses)
|
||||
(if (= nil clauses)
|
||||
nil
|
||||
(if (= (first (first clauses)) 'else)
|
||||
(if (= (car (car clauses)) 'else)
|
||||
(begin
|
||||
(if (not (= (rest clauses) ()))
|
||||
(if (not (= (cdr clauses) ()))
|
||||
(error :syntax-error "There are additional clauses after the else clause!")
|
||||
(pair 'begin (rest (first clauses)))))
|
||||
`(if (member? ,var ',(first (first clauses)))
|
||||
(begin ,@(rest (first clauses)))
|
||||
,(rec (rest clauses))))))
|
||||
(cons 'begin (cdr (car clauses)))))
|
||||
`(if (member? ,var ',(car (car clauses)))
|
||||
(begin ,@(cdr (car clauses)))
|
||||
,(rec (cdr clauses))))))
|
||||
(rec clauses))
|
||||
|
||||
(define-syntax (construct-list . body)
|
||||
(define-macro (construct-list . body)
|
||||
"
|
||||
{{{example_start}}}
|
||||
(construct-list
|
||||
@@ -152,7 +156,7 @@ condition is false."
|
||||
(construct-list
|
||||
i <- '(1 2 3 4)
|
||||
j <- '(A B)
|
||||
yield (pair i j))
|
||||
yield (cons i j))
|
||||
|
||||
(construct-list
|
||||
i <- '(1 2 3 4 5 6 7 8)
|
||||
@@ -161,44 +165,44 @@ condition is false."
|
||||
"
|
||||
(define (append-map f ll)
|
||||
(unless (= ll ())
|
||||
(define val (f (first ll)))
|
||||
(if (= (first val) ())
|
||||
(append-map f (rest ll))
|
||||
(define val (f (car ll)))
|
||||
(if (= (car val) ())
|
||||
(append-map f (cdr ll))
|
||||
(extend
|
||||
val
|
||||
(append-map f (rest ll))))))
|
||||
(append-map f (cdr ll))))))
|
||||
|
||||
(define (rec body)
|
||||
(cond
|
||||
((= () body) ())
|
||||
((= () (rest body)) (first body))
|
||||
((= (first (rest body)) '<-)
|
||||
`(,append-map (lambda (,(first body)) (list ,(rec (rest (rest (rest body)))))) ,(first (rest (rest body)))))
|
||||
((= (first body) 'if)
|
||||
`(when ,(first (rest body)) ,(rec (rest (rest body)))))
|
||||
((= (first (rest body)) 'yield)
|
||||
(first (rest body)))
|
||||
((= () (cdr body)) (car body))
|
||||
((= (car (cdr body)) '<-)
|
||||
`(,append-map (lambda (,(car body)) (list ,(rec (cdr (cdr (cdr body)))))) ,(car (cdr (cdr body)))))
|
||||
((= (car body) 'if)
|
||||
`(when ,(car (cdr body)) ,(rec (cdr (cdr body)))))
|
||||
((= (car (cdr body)) 'yield)
|
||||
(car (cdr body)))
|
||||
(else (error :syntax-error "Not a do-able expression"))))
|
||||
|
||||
(rec body))
|
||||
|
||||
(define-syntax (define-typed args . body)
|
||||
(define-macro (define-typed args . body)
|
||||
(define (get-arg-names args)
|
||||
(when args
|
||||
(pair (first args)
|
||||
(get-arg-names (rest (rest args))))))
|
||||
(let ((name (first args))
|
||||
(lambda-list (rest args))
|
||||
(arg-names (get-arg-names (rest args))))
|
||||
(cons (car args)
|
||||
(get-arg-names (cdr (cdr args))))))
|
||||
(let ((name (car args))
|
||||
(lambda-list (cdr args))
|
||||
(arg-names (get-arg-names (cdr args))))
|
||||
`(define (,name ,@arg-names)
|
||||
(assert-types= ,@lambda-list)
|
||||
,@body)))
|
||||
|
||||
|
||||
(define-syntax (define-module module-name (:imports ()) (:exports ()) . body)
|
||||
(define-macro (define-module module-name (:imports ()) (:exports ()) . body)
|
||||
(let ((module-prefix (concat-strings (symbol->string module-name) "::")))
|
||||
(eval `(,begin ,@(map (lambda (x) `(,import ,x)) imports) ,@body))
|
||||
(pair 'begin
|
||||
(cons 'begin
|
||||
(map (lambda (orig-export-name)
|
||||
((lambda (export-name)
|
||||
`(define ,export-name
|
||||
@@ -213,7 +217,7 @@ condition is false."
|
||||
(exec `(begin ,@(map (lambda (x) `(,import ,x)) imports) ,@body)))
|
||||
(eval exec)
|
||||
(enable-debug-log)
|
||||
(pair begin
|
||||
(cons begin
|
||||
(map (lambda (orig-export-name)
|
||||
((lambda (export-name)
|
||||
`(define ,export-name
|
||||
@@ -224,24 +228,24 @@ condition is false."
|
||||
exports))
|
||||
(disable-debug-log)))
|
||||
|
||||
(define-syntax (generic-extend args . body)
|
||||
(let ((fun-name (first args))
|
||||
(params (rest args))
|
||||
(define-macro (generic-extend args . body)
|
||||
(let ((fun-name (car args))
|
||||
(params (cdr args))
|
||||
(types ())
|
||||
(names ()))
|
||||
(define (process-params params)
|
||||
(when params
|
||||
(let ((_name (first params))
|
||||
(_type (first (rest params))))
|
||||
(let ((_name (car params))
|
||||
(_type (car (cdr params))))
|
||||
(assert (symbol? _name))
|
||||
(assert (keyword? _type))
|
||||
(set! types (append types _type))
|
||||
(set! names (append names _name))
|
||||
(process-params (rest (rest params))))))
|
||||
(process-params (cdr (cdr params))))))
|
||||
(process-params params)
|
||||
;; we have the fun-name, the param names and the types, lets go:
|
||||
;;
|
||||
;; first check if there is already a generic-<name>-map
|
||||
;; car check if there is already a generic-<name>-map
|
||||
(let ((generic-map-name (string->symbol
|
||||
(concat-strings "generic-" (symbol->string fun-name) "-map"))))
|
||||
(unless (bound? generic-map-name)
|
||||
@@ -279,10 +283,10 @@ condition is false."
|
||||
(define (types=? . objs)
|
||||
(define (inner objs)
|
||||
(if objs
|
||||
(let ((actual-type (type (first objs)))
|
||||
(desired-type (first (rest objs))))
|
||||
(let ((actual-type (type (car objs)))
|
||||
(desired-type (car (cdr objs))))
|
||||
(if (= actual-type desired-type)
|
||||
(inner (rest (rest objs)))
|
||||
(inner (cdr (cdr objs)))
|
||||
()))
|
||||
t))
|
||||
(inner objs))
|
||||
@@ -290,10 +294,10 @@ condition is false."
|
||||
(define (assert-types= . objs)
|
||||
(define (inner objs)
|
||||
(when objs
|
||||
(let ((actual-type (type (first objs)))
|
||||
(desired-type (first (rest objs))))
|
||||
(let ((actual-type (type (car objs)))
|
||||
(desired-type (car (cdr objs))))
|
||||
(if (= actual-type desired-type)
|
||||
(inner (rest (rest objs)))
|
||||
(inner (cdr (cdr objs)))
|
||||
(error :type-missmatch "type missmatch" actual-type desired-type)))))
|
||||
(inner objs))
|
||||
|
||||
@@ -309,7 +313,7 @@ condition is false."
|
||||
"Checks if the argument is a keyword."
|
||||
(type=? x :keyword))
|
||||
|
||||
(define (pair? x)
|
||||
(define (cons? x)
|
||||
"Checks if the argument is a pair."
|
||||
(type=? x :pair))
|
||||
|
||||
@@ -352,70 +356,70 @@ condition is false."
|
||||
(print (end a))
|
||||
{{{example_end}}}
|
||||
"
|
||||
(if (or (null? seq) (not (pair? (rest seq))))
|
||||
(if (or (null? seq) (not (cons? (cdr seq))))
|
||||
seq
|
||||
(end (rest seq))))
|
||||
(end (cdr seq))))
|
||||
|
||||
(define (last seq)
|
||||
"Returns the (first) of the last (pair) of the given sequence.
|
||||
"Returns the (car) of the last (cons) of the given sequence.
|
||||
|
||||
{{{example_start}}}
|
||||
(define a (list 1 2 3 4))
|
||||
(print (last a))
|
||||
{{{example_end}}}
|
||||
"
|
||||
(first (end seq)))
|
||||
(car (end seq)))
|
||||
|
||||
(define (extend seq elem)
|
||||
"Extends a list with the given element, by putting it in
|
||||
the (rest) of the last element of the sequence."
|
||||
(if (pair? seq)
|
||||
the (cdr ) of the last element of the sequence."
|
||||
(if (cons? seq)
|
||||
(begin
|
||||
(define e (end seq))
|
||||
(mutate e (pair (first e) elem))
|
||||
(mutate! e (cons (car e) elem))
|
||||
seq)
|
||||
elem))
|
||||
|
||||
(define (extend2 seq elem)
|
||||
"Extends a list with the given element, by putting it in
|
||||
the (rest) of the last element of the sequence."
|
||||
the (cdr ) of the last element of the sequence."
|
||||
(print "addr of (end seq)" (addr-of (end seq)))
|
||||
(if (pair? seq)
|
||||
(if (cons? seq)
|
||||
(let ((e (end seq)))
|
||||
(print "addr if e inner" (addr-of e))
|
||||
(mutate e (pair (first e) elem))
|
||||
(mutate! e (cons (car e) elem))
|
||||
seq))
|
||||
elem)
|
||||
|
||||
(define (append seq elem)
|
||||
"Appends an element to a sequence, by extendeing the list
|
||||
with (pair elem nil)."
|
||||
(extend seq (pair elem ())))
|
||||
with (cons elem nil)."
|
||||
(extend seq (cons elem ())))
|
||||
|
||||
(define (length seq)
|
||||
"Returns the length of the given sequence."
|
||||
(if (null? seq)
|
||||
0
|
||||
(+ 1 (length (rest seq)))))
|
||||
(+ 1 (length (cdr seq)))))
|
||||
|
||||
(define (member? elem seq)
|
||||
(when (pair? seq)
|
||||
(if (= elem (first seq))
|
||||
(when (cons? seq)
|
||||
(if (= elem (car seq))
|
||||
t
|
||||
(member? elem (rest seq)))))
|
||||
(member? elem (cdr seq)))))
|
||||
|
||||
(define (sublist-starting-at-index seq index)
|
||||
(cond ((< index 0)
|
||||
(error :index-out-of-range "sublist-starting-at-index: index must be positive"))
|
||||
((null? seq) ())
|
||||
((= 0 index) seq)
|
||||
(else (sublist-starting-at (rest seq) (- index 1)))))
|
||||
(else (sublist-starting-at (cdr seq) (- index 1)))))
|
||||
|
||||
(define (list-without-index seq index)
|
||||
(cond ((or (< index 0) (null? seq))
|
||||
(error :index-out-of-range "list-remove-index!: index out of range"))
|
||||
((= 0 index) (rest seq))
|
||||
(else (pair (first seq) (list-without-index (rest seq) (- index 1))))))
|
||||
((= 0 index) (cdr seq))
|
||||
(else (cons (car seq) (list-without-index (cdr seq) (- index 1))))))
|
||||
|
||||
(define (increment val)
|
||||
"Adds one to the argument."
|
||||
@@ -429,7 +433,7 @@ with (pair elem nil)."
|
||||
"Returns a sequence of numbers starting with the number defined
|
||||
by the key =from= and ends with the number defined in =to=."
|
||||
(when (< from to)
|
||||
(pair from (range :from (+ 1 from) :to to))))
|
||||
(cons from (range :from (+ 1 from) :to to))))
|
||||
|
||||
(define (range-while (:from 0) :to)
|
||||
"Returns a sequence of numbers starting with the number defined
|
||||
@@ -439,19 +443,19 @@ by the key 'from' and ends with the number defined in 'to'."
|
||||
(set! from (increment from))
|
||||
(while (< from to)
|
||||
(begin
|
||||
(mutate head (pair (first head) (pair (copy from) nil)))
|
||||
(define head (rest head))
|
||||
(mutate! head (cons (car head) (cons (copy from) nil)))
|
||||
(define head (cdr head))
|
||||
(set! from (increment from))))
|
||||
result)
|
||||
|
||||
(define (map fun seq)
|
||||
"Takes a function and a sequence as arguments and returns a new
|
||||
sequence which contains the results of using the first sequences
|
||||
sequence which contains the results of using the car sequences
|
||||
elemens as argument to that function."
|
||||
(if (null? seq)
|
||||
seq
|
||||
(pair (fun (first seq))
|
||||
(map fun (rest seq)))))
|
||||
(cons (fun (car seq))
|
||||
(map fun (cdr seq)))))
|
||||
|
||||
(define (reduce fun seq)
|
||||
"Takes a function and a sequence as arguments and applies the
|
||||
@@ -466,10 +470,10 @@ instead."
|
||||
function to the argument sequence. reduce-binary applies the arguments
|
||||
*pair-wise* which means it works with binary functions as compared to
|
||||
[[=reduce=]]."
|
||||
(if (null? (rest seq))
|
||||
(first seq)
|
||||
(fun (first seq)
|
||||
(reduce-binary fun (rest seq)))))
|
||||
(if (null? (cdr seq))
|
||||
(car seq)
|
||||
(fun (car seq)
|
||||
(reduce-binary fun (cdr seq)))))
|
||||
|
||||
(define (filter fun seq)
|
||||
"Takes a function and a sequence as arguments and applies the
|
||||
@@ -477,25 +481,25 @@ function to every value in the sequence. If the result of that
|
||||
funciton application returns a truthy value, the original value is
|
||||
added to a list, which in the end is returned."
|
||||
(when seq
|
||||
(if (fun (first seq))
|
||||
(pair (first seq)
|
||||
(filter fun (rest seq)))
|
||||
(filter fun (rest seq)))))
|
||||
(if (fun (car seq))
|
||||
(cons (car seq)
|
||||
(filter fun (cdr seq)))
|
||||
(filter fun (cdr seq)))))
|
||||
|
||||
|
||||
(define (zip l1 l2)
|
||||
(unless (and (null? l1) (null? l2))
|
||||
(pair (list (first l1) (first l2))
|
||||
(zip (rest l1) (rest l2)))))
|
||||
(cons (list (car l1) (car l2))
|
||||
(zip (cdr l1) (cdr l2)))))
|
||||
|
||||
(define (unzip lists)
|
||||
(when lists
|
||||
(define (iter lists l1 l2)
|
||||
(define elem (first lists))
|
||||
(define elem (car lists))
|
||||
(if elem
|
||||
(iter (rest lists)
|
||||
(pair (first elem) l1)
|
||||
(pair (first (rest elem)) l2))
|
||||
(iter (cdr lists)
|
||||
(cons (car elem) l1)
|
||||
(cons (car (cdr elem)) l2))
|
||||
(list l1 l2)))
|
||||
|
||||
(iter lists () ())))
|
||||
@@ -503,8 +507,8 @@ added to a list, which in the end is returned."
|
||||
(define (enumerate seq)
|
||||
(define (enumerate-inner seq next-num)
|
||||
(when seq
|
||||
(pair (list (first seq) next-num)
|
||||
(enumerate-inner (rest seq) (+ 1 next-num)))))
|
||||
(cons (list (car seq) next-num)
|
||||
(enumerate-inner (cdr seq) (+ 1 next-num)))))
|
||||
(enumerate-inner seq 0))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user