work transfer to laptop

This commit is contained in:
Felix Brendel
2019-09-19 09:20:26 +02:00
parent 5a117d94ee
commit 0c73728217
22 changed files with 3018 additions and 3090 deletions
+71 -72
View File
@@ -1,10 +1,8 @@
(define-syntax (pe expr)
`(printf ',expr "evaluates to" ,expr))
`(print ',expr "evaluates to" ,expr))
(define-syntax (when condition :rest body)
"Special form for when multiple actions should be done if a
(define-syntax (when condition . body)
:doc "Special form for when multiple actions should be done if a
condition is true.
{{{example_start}}}
@@ -22,22 +20,23 @@ condition is true.
`(if ,condition @body nil)
`(if ,condition (begin @body) nil)))
(define-syntax (unless condition :rest body)
"Special form for when multiple actions should be done if a
(define-syntax (unless condition . body)
:doc "Special form for when multiple actions should be done if a
condition is false."
(if (= (rest body) ())
`(if ,condition nil @body)
`(if ,condition nil (begin @body))))
(define-syntax (n-times times action)
"Executes action times times."
:doc "Executes action times times."
(define (repeat times elem)
(unless (> 1 times)
(pair elem (repeat (- times 1) elem))))
`(begin @(repeat times action)))
(define-syntax (let bindings :rest body)
(define-syntax (let bindings . body)
(define (unzip lists)
(break)
(when lists
(define (iter lists l1 l2)
(define elem (first lists))
@@ -51,9 +50,9 @@ condition is false."
(define unzipped (unzip bindings))
`((lambda ,(first unzipped) @body) @(first (rest unzipped))))
`((,lambda ,(first unzipped) @body) @(first (rest unzipped))))
(define-syntax (cond :rest clauses)
(define-syntax (cond . clauses)
(define (rec clauses)
(if (= nil clauses)
nil
@@ -68,7 +67,7 @@ condition is false."
(rec clauses))
(define-syntax (case var :rest clauses)
(define-syntax (case var . clauses)
(define (rec clauses)
(if (= nil clauses)
nil
@@ -82,11 +81,11 @@ condition is false."
,(rec (rest clauses))))))
(rec clauses))
(define-syntax (define-special name-and-args :rest body)
(define-syntax (define-special name-and-args . body)
`(define ,(first name-and-args) (special-lambda ,(rest name-and-args) @body)))
(define-syntax (construct-list :rest body)
"
(define-syntax (construct-list . body)
:doc "
{{{example_start}}}
(construct-list
i <- '(1 2 3 4 5)
@@ -127,11 +126,11 @@ condition is false."
(rec body))
(define-syntax (apply fun seq)
"Applies the function to the sequence, as in calls the function with
:doc "Applies the function to the sequence, as in calls the function with
ithe sequence as arguemens."
`(eval (pair ,fun ,seq)))
(define-syntax (define-typed args :rest body)
(define-syntax (define-typed args . body)
(define (get-arg-names args)
(when args
(pair (first args)
@@ -144,7 +143,7 @@ ithe sequence as arguemens."
@body)))
(define-syntax (define-module module-name :keys exports :rest body)
(define-syntax (define-module module-name :exports . body)
(let ((module-prefix (concat-strings (symbol->string module-name) "::")))
(eval `(begin @body))
(pair 'begin
@@ -159,14 +158,14 @@ ithe sequence as arguemens."
(define (null? x)
"Checks if the argument is =nil=."
:doc "Checks if the argument is =nil=."
(= x ()))
(define (type=? obj typ)
"Checks if the argument =obj= is of type =typ="
:doc "Checks if the argument =obj= is of type =typ="
(= (type obj) typ))
(define (types=? :rest objs)
(define (types=? . objs)
(define (inner objs)
(if objs
(let ((actual-type (type (first objs)))
@@ -177,7 +176,7 @@ ithe sequence as arguemens."
t))
(inner objs))
(define (assert-types= :rest objs)
(define (assert-types= . objs)
(define (inner objs)
(when objs
(let ((actual-type (type (first objs)))
@@ -188,39 +187,39 @@ ithe sequence as arguemens."
(inner objs))
(define (number? x)
"Checks if the argument is a number."
:doc "Checks if the argument is a number."
(type=? x :number))
(define (symbol? x)
"Checks if the argument is a symbol."
:doc "Checks if the argument is a symbol."
(type=? x :symbol))
(define (keyword? x)
"Checks if the argument is a keyword."
:doc "Checks if the argument is a keyword."
(type=? x :keyword))
(define (pair? x)
"Checks if the argument is a pair."
:doc "Checks if the argument is a pair."
(type=? x :pair))
(define (string? x)
"Checks if the argument is a string."
:doc "Checks if the argument is a string."
(type=? x :string))
(define (lambda? x)
"Checks if the argument is a function."
:doc "Checks if the argument is a function."
(type=? x :lambda))
(define (macro? x)
"Checks if the argument is a macro."
:doc "Checks if the argument is a macro."
(type=? x :macro))
(define (special-lambda? x)
"Checks if the argument is a special-lambda."
:doc "Checks if the argument is a special-lambda."
(type=? x :dynamic-macro))
(define (built-in-function? x)
"Checks if the argument is a built-in function."
:doc "Checks if the argument is a built-in function."
(type=? x :built-in-function))
(define (callable? x)
@@ -230,11 +229,11 @@ ithe sequence as arguemens."
(built-in-function? x)))
(define (end seq)
"Returns the last pair in the sqeuence.
:doc "Returns the last pair in the sqeuence.
{{{example_start}}}
(define a (list 1 2 3 4))
(printf (end a))
(print (end a))
{{{example_end}}}
"
(if (or (null? seq) (not (pair? (rest seq))))
@@ -242,17 +241,17 @@ ithe sequence as arguemens."
(end (rest seq))))
(define (last seq)
"Returns the (first) of the last (pair) of the given sequence.
:doc "Returns the (first) of the last (pair) of the given sequence.
{{{example_start}}}
(define a (list 1 2 3 4))
(printf (last a))
(print (last a))
{{{example_end}}}
"
(first (end seq)))
(define (extend seq elem)
"Extends a list with the given element, by putting it in
:doc "Extends a list with the given element, by putting it in
the (rest) of the last element of the sequence."
(if (pair? seq)
(begin
@@ -262,23 +261,23 @@ the (rest) of the last element of the sequence."
elem))
(define (extend2 seq elem)
"Extends a list with the given element, by putting it in
:doc "Extends a list with the given element, by putting it in
the (rest) of the last element of the sequence."
(printf "addr of (end seq)" (addr-of (end seq)))
(print "addr of (end seq)" (addr-of (end seq)))
(if (pair? seq)
(let ((e (end seq)))
(printf "addr if e inner" (addr-of e))
(print "addr if e inner" (addr-of e))
(mutate e (pair (first e) elem))
seq))
elem)
(define (append seq elem)
"Appends an element to a sequence, by extendeing the list
:doc "Appends an element to a sequence, by extendeing the list
with (pair elem nil)."
(extend seq (pair elem ())))
(define (length seq)
"Returns the length of the given sequence."
:doc "Returns the length of the given sequence."
(if (null? seq)
0
(+ 1 (length (rest seq)))))
@@ -303,21 +302,21 @@ with (pair elem nil)."
(else (pair (first seq) (list-without-index (rest seq) (- index 1))))))
(define (increment val)
"Adds one to the argument."
:doc "Adds one to the argument."
(+ val 1))
(define (decrement val)
"Subtracts one from the argument."
:doc "Subtracts one from the argument."
(- val 1))
(define (range :keys from :defaults-to 0 to)
"Returns a sequence of numbers starting with the number defined by the
key =from= and ends with the number defined in =to=."
(define (range (:from 0) :to)
:doc "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))))
(define (range-while :keys from :defaults-to 0 to)
"Returns a sequence of numbers starting with the number defined
(define (range-while (:from 0) to)
:doc "Returns a sequence of numbers starting with the number defined
by the key 'from' and ends with the number defined in 'to'."
(define result (list (copy from)))
(define head result)
@@ -330,7 +329,7 @@ by the key 'from' and ends with the number defined in 'to'."
result)
(define (map fun seq)
"Takes a function and a sequence as arguments and returns a new
:doc "Takes a function and a sequence as arguments and returns a new
sequence which contains the results of using the first sequences
elemens as argument to that function."
(if (null? seq)
@@ -339,17 +338,17 @@ elemens as argument to that function."
(map fun (rest seq)))))
(define (reduce fun seq)
"Takes a function and a sequence as arguments and applies the
function to the argument sequence. This only works correctly if the
given function accepts a variable amount of parameters. If your
funciton is limited to two arguments, use [[=reduce-binary=]]
:doc "Takes a function and a sequence as arguments and applies the
function to the argument sequence. This only works correctly if the
given function accepts a variable amount of parameters. If your
funciton is limited to two arguments, use [[=reduce-binary=]]
instead."
(apply fun seq))
(define (reduce-binary fun seq)
"Takes a function and a sequence as arguments and applies the
:doc "Takes a function and a sequence as arguments and applies the
function to the argument sequence. reduce-binary applies the arguments
*pair-wise* which means it works with binary functions as compared to
*pair-wise* which means it works with binary functions as compared to
[[=reduce=]]."
(if (null? (rest seq))
(first seq)
@@ -357,7 +356,7 @@ function to the argument sequence. reduce-binary applies the arguments
(reduce-binary fun (rest seq)))))
(define (filter fun seq)
"Takes a function and a sequence as arguments and applies the
:doc "Takes a function and a sequence as arguments and applies the
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."
@@ -392,20 +391,20 @@ added to a list, which in the end is returned."
(enumerate-inner (rest seq) (+ 1 next-num)))))
(enumerate-inner seq 0))
(define (printf :keys sep :defaults-to " " end :defaults-to "\n" :rest args)
"A wrapper for the built-in function [[=print=]] that accepts a
variable number of arguments and also provides keywords for specifying
the printed separators (=sep=) between the arguments and what should
be printed after the last argument (=end=)."
(define (printf-inner args)
(if args
(begin
(print (first args))
(when (rest args)
(print sep))
(printf-inner (rest args)))
; else
(print end)))
;; (define (print (:sep " ") (:end "\n") . args)
;; "A wrapper for the built-in function [[=print=]] that accepts a
;; variable number of arguments and also provides keywords for specifying
;; the printed separators (=sep=) between the arguments and what should
;; be printed after the last argument (=end=)."
;; (define (print-inner args)
;; (if args
;; (begin
;; (print (first args))
;; (when (rest args)
;; (print sep))
;; (print-inner (rest args)))
;; ; else
;; (print end)))
(printf-inner args)
())
;; (print-inner args)
;; ())