Getting the manual going

This commit is contained in:
Felix Brendel
2019-05-16 16:52:37 +02:00
parent fb7f3a00c9
commit 03a2c81aa5
8 changed files with 598 additions and 58 deletions
+15 -34
View File
@@ -5,26 +5,12 @@
(define-syntax (unless condition :rest body)
`(if ,condition nil ,(pair begin body)))
;; (define-syntax defun (name arguments :rest body)
;; ;; (type-assert arguments :pair)
;; ;; `(define ,name (lambda ,arguments ,body))
;; ;; TODO(Felix: I think we do not need to wrap the body of the lamba
;; ;; in a begin
;; ;; see if we have a docstring
;; (if (and (= (type (first body)) :string) (not (= (type (rest body)) :nil)))
;; (list 'define name (list 'lambda arguments (first body) (pair 'begin (rest body))))
;; (list 'define name (list 'lambda arguments (pair 'begin body)))))
;; (define-syntax defspecial (name arguments :rest body)
;; ;; (type-assert arguments :pair)
;; ;; `(define ,name (lambda ,arguments ,body))
;; ;; see if we have a docstring
;; (if (and (= (type (first body)) :string) (not (= (type (rest body)) :nil)))
;; (list 'define name (list 'special-lambda arguments (first body) (pair 'begin (rest body))))
;; (list 'define name (list 'special-lambda arguments (pair 'begin body)))))
(define-syntax (n-times times action)
"Executes action times times."
(define (repeat times elem)
(unless (> 1 times)
(pair elem (repeat (- times 1) elem))))
(pair 'begin (repeat times action)))
;; (define (fib n))
;; (define-syntax define (name :rest value)
@@ -41,7 +27,6 @@
;; (print "\nbb\n")
;; (pair 'define (pair name value)))))
;; TODO(Felix): else symbol
(define-syntax (cond :rest clauses)
(define (rec clauses)
(if (= nil clauses)
@@ -122,13 +107,6 @@ the (rest) of the last element of the sequence."
with (pair elem nil)."
(extend seq (pair elem nil)))
(define-syntax (extend! seq elem)
"test"
`(mutate ,seq (extend ,seq ,elem)))
(define-syntax (append! seq elem)
`(mutate ,seq (append ,seq ,elem)))
(define (length seq)
"Returns the length of the given sequence."
(if (nil? seq)
@@ -143,11 +121,6 @@ with (pair elem nil)."
"Subtracts one from the argument."
(- val 1))
;; (defmacro n-times (@times @action)
;; "Executes @action @times times."
;; (unless (<= (eval @times) 0)
;; (eval @action)
;; (apply n-times (list (list - @times 1) @action))))
;; (defmacro for (@symbol @from @to :rest @for-body)
;; "Designed to resemble a C style for loop. It takes a symbol as
@@ -226,6 +199,14 @@ added to a list, which in the end is returned."
nil
(pair (list (first l1) (first l2)) (zip (rest l1) (rest l2)))))
(define (enumerate seq)
(define (enumerate-inner seq next-num)
(when seq
(pair (list (first seq) next-num)
(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 (print) that accepts a variable number
of arguments and also provides keywords for specifying the printed
@@ -244,4 +225,4 @@ las argument."
(eval (pair printf-quoted (extend (list :@sep (eval sep) :@end (eval end)) args))))
(define-syntax (pe expr)
`(printf ',expr "evaluates to" ,(eval expr)))
`(printf ',expr "evaluates to" ,expr))
+8 -6
View File
@@ -29,16 +29,16 @@ the (rest) of the last element of the sequence." (if (pair? seq) (begin (define
(define (append seq elem) "Appends an element to a sequence, by extendeing the list
with (pair elem nil)." (extend seq (pair elem nil)))
(define (length seq) "Returns the length of the given sequence." (if (nil? seq) 0.000000 (+ 1.000000 (length (rest seq)))))
(define (length seq) "Returns the length of the given sequence." (if (nil? seq) 0 (+ 1 (length (rest seq)))))
(define (increment val) "Adds one to the argument." (+ val 1.000000))
(define (increment val) "Adds one to the argument." (+ val 1))
(define (decrement val) "Subtracts one from the argument." (- val 1.000000))
(define (decrement val) "Subtracts one from the argument." (- val 1))
(define (range :keys from :defaults-to 0.000000 to) "Returns a sequence of numbers starting with the number defined
by the key 'from' and ends with the number defined in 'to'." (if (< from to) ([C-function] (pair from (range :from (+ 1.000000 from) :to to))) nil))
(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'." (if (< from to) ([C-function] (pair from (range :from (+ 1 from) :to to))) nil))
(define (range-while :keys from :defaults-to 0.000000 to) "Returns a sequence of numbers starting with the number defined
(define (range-while :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 result (list (copy from))) (define head result) (mutate from (increment from)) (while (< from to) (begin (mutate head (pair (first head) (pair (copy from) nil))) (define head (rest head)) (mutate from (increment from)))) result)
(define (map fun seq) "Takes a function and a sequence as arguments and returns a new
@@ -63,6 +63,8 @@ added to a list, which in the end is returned." (if seq ([C-function] (if (fun (
(define (zip l1 l2) (if (and (nil? l1) (nil? l2)) nil (pair (list (first l1) (first l2)) (zip (rest l1) (rest l2)))))
(define (enumerate seq) (define (enumerate-inner seq next-num) (if seq ([C-function] (pair (list (first seq) next-num) (enumerate-inner (rest seq) (+ 1 next-num)))) nil)) (enumerate-inner seq 0))
(define (printf :keys sep :defaults-to " " end :defaults-to "
" :rest args) "A wrapper for the built-in (print) that accepts a variable number
of arguments and also provides keywords for specifying the printed
+3 -3
View File
@@ -2,11 +2,11 @@
(define (make-vector3 _x _y _z) "This is the handle to an object of the class vector3" (let ((x _x) (y _y) (z _z)) (define (get-x) x) (define (get-y) y) (define (get-z) z) (define (set-x new-x) (mutate x new-x)) (define (set-y new-y) (mutate y new-y)) (define (set-z new-z) (mutate z new-z)) (define (length) (** (+ (* x x) (* y y) (* z z)) 0.500000)) (define (scale fac) (mutate x (* fac x)) (mutate y (* fac y)) (mutate z (* fac z)) fac) (define (add other) (make-vector3 (+ x (other get-x)) (+ y (other get-y)) (+ z (other get-z)))) (define (subtract other) (make-vector3 (- x (other get-x)) (- y (other get-y)) (- z (other get-z)))) (define (scalar-product other) (+ (* x (other get-x)) (* y (other get-y)) (* z (other get-z)))) (define (cross-product other) (make-vector3 (- (* y (other get-z)) (* z (other get-y))) (- (* z (other get-x)) (* x (other get-z))) (- (* x (other get-y)) (* y (other get-x))))) (define (print) (printf :sep "" "[vector3] (" x y z ")")) (type-wrap (special-lambda (message :rest args) "This is the docs for the handle" (eval (extend (list message) args))) :vector3)))
(define v1 (make-vector3 1.000000 2.000000 3.000000))
(define v1 (make-vector3 1 2 3))
(define v2 (make-vector3 3.000000 2.000000 1.000000))
(define v2 (make-vector3 3 2 1))
(assert (= (type v1) (type v2) :vector3))
(assert (= (v1 scalar-product v2) 10.000000))
(assert (= (v1 scalar-product v2) 10))
+7 -7
View File
@@ -1,18 +1,18 @@
(define (make-counter) (let ((var 0.000000)) (lambda () (mutate var (+ 1.000000 var)) var)))
(define (make-counter) (let ((var 0)) (lambda () (mutate var (+ 1 var)) var)))
(define counter1 (make-counter))
(assert (= (counter1) 1.000000))
(assert (= (counter1) 1))
(define counter2 (make-counter))
(assert (= (counter2) 1.000000))
(assert (= (counter2) 1))
(assert (= (counter2) 2.000000))
(assert (= (counter2) 2))
(assert (= (counter1) 2.000000))
(assert (= (counter1) 2))
(assert (= (counter1) 3.000000))
(assert (= (counter1) 3))
(assert (= (counter2) 3.000000))
(assert (= (counter2) 3))