lota things
This commit is contained in:
+72
-31
@@ -2,12 +2,27 @@
|
||||
`(printf ',expr "evaluates to" ,expr))
|
||||
|
||||
(define-syntax (when condition :rest body)
|
||||
"Doc String for `when'"
|
||||
"Special form for when multiple actions should be done if a
|
||||
condition is true.
|
||||
|
||||
{{{example_start}}}
|
||||
(when (not ())
|
||||
(print \"Hello \")
|
||||
(print \"from \")
|
||||
(print \"when!\"))
|
||||
|
||||
(when ()
|
||||
(print \"Goodbye \")
|
||||
(print \"World!\"))
|
||||
{{{example_end}}}
|
||||
"
|
||||
(if (= (rest body) ())
|
||||
`(if ,condition @body)
|
||||
`(if ,condition (begin @body))))
|
||||
|
||||
(define-syntax (unless condition :rest body)
|
||||
"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))))
|
||||
@@ -55,9 +70,11 @@
|
||||
|
||||
(define-syntax (construct-list :rest body)
|
||||
"
|
||||
{{{example_start}}}
|
||||
(construct-list
|
||||
i <- '(1 2 3 4 5)
|
||||
yield (* i i))
|
||||
{{{example_end}}}
|
||||
|
||||
(construct-list
|
||||
i <- '(1 2 3 4)
|
||||
@@ -68,7 +85,6 @@
|
||||
i <- '(1 2 3 4 5 6 7 8)
|
||||
when (evenp i)
|
||||
yield i)
|
||||
|
||||
"
|
||||
(define (append-map f ll)
|
||||
(unless (= ll ())
|
||||
@@ -113,7 +129,7 @@ ithe sequence as arguemens."
|
||||
:package)))))
|
||||
|
||||
|
||||
(define (nil? x)
|
||||
(define (null? x)
|
||||
"Checks if the argument is =nil=."
|
||||
(= x nil))
|
||||
|
||||
@@ -160,13 +176,25 @@ ithe sequence as arguemens."
|
||||
(built-in-function? x)))
|
||||
|
||||
(define (end seq)
|
||||
"Returns the last pair in the sqeuence."
|
||||
(if (or (nil? seq) (not (pair? (rest seq))))
|
||||
"Returns the last pair in the sqeuence.
|
||||
|
||||
{{{example_start}}}
|
||||
(define a (list 1 2 3 4))
|
||||
(printf (end a))
|
||||
{{{example_end}}}
|
||||
"
|
||||
(if (or (null? seq) (not (pair? (rest seq))))
|
||||
seq
|
||||
(end (rest seq))))
|
||||
|
||||
(define (last seq)
|
||||
"Returns the (first) of the last (pair) of the given sequence."
|
||||
"Returns the (first) of the last (pair) of the given sequence.
|
||||
|
||||
{{{example_start}}}
|
||||
(define a (list 1 2 3 4))
|
||||
(printf (last a))
|
||||
{{{example_end}}}
|
||||
"
|
||||
(first (end seq)))
|
||||
|
||||
(define (extend seq elem)
|
||||
@@ -197,10 +225,23 @@ with (pair elem nil)."
|
||||
|
||||
(define (length seq)
|
||||
"Returns the length of the given sequence."
|
||||
(if (nil? seq)
|
||||
(if (null? seq)
|
||||
0
|
||||
(+ 1 (length (rest seq)))))
|
||||
|
||||
(define (sublist-starting-at-index seq index)
|
||||
(cond ((< index 0)
|
||||
(error "sublist-starting-at-index: index must be positive"))
|
||||
((null? seq) ())
|
||||
((= 0 index) seq)
|
||||
(else (sublist-starting-at (rest seq) (- index 1)))))
|
||||
|
||||
(define (list-without-index seq index)
|
||||
(cond ((or (< index 0) (null? seq))
|
||||
(error "list-remove-index!: index out of range"))
|
||||
((= 0 index) (rest seq))
|
||||
(else (pair (first seq) (list-without-index (rest seq) (- index 1))))))
|
||||
|
||||
(define (increment val)
|
||||
"Adds one to the argument."
|
||||
(+ val 1))
|
||||
@@ -232,7 +273,7 @@ by the key 'from' and ends with the number defined in 'to'."
|
||||
"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 (nil? seq)
|
||||
(if (null? seq)
|
||||
seq
|
||||
(pair (fun (first seq))
|
||||
(map fun (rest seq)))))
|
||||
@@ -246,11 +287,11 @@ instead."
|
||||
(apply fun seq))
|
||||
|
||||
(define (reduce-binary fun seq)
|
||||
"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 `reduce'."
|
||||
(if (nil? (rest seq))
|
||||
"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
|
||||
[[=reduce=]]."
|
||||
(if (null? (rest seq))
|
||||
(first seq)
|
||||
(fun (first seq)
|
||||
(reduce-binary fun (rest seq)))))
|
||||
@@ -268,7 +309,7 @@ added to a list, which in the end is returned."
|
||||
|
||||
|
||||
(define (zip l1 l2)
|
||||
(unless (and (nil? l1) (nil? l2))
|
||||
(unless (and (null? l1) (null? l2))
|
||||
(pair (list (first l1) (first l2))
|
||||
(zip (rest l1) (rest l2)))))
|
||||
|
||||
@@ -280,9 +321,9 @@ added to a list, which in the end is returned."
|
||||
(iter (rest lists)
|
||||
(pair (first elem) l1)
|
||||
(pair (first (rest elem)) l2))
|
||||
(list l1 l2))))
|
||||
(list l1 l2)))
|
||||
|
||||
(iter lists () ()))
|
||||
(iter lists () ())))
|
||||
|
||||
(define (enumerate seq)
|
||||
(define (enumerate-inner seq next-num)
|
||||
@@ -291,20 +332,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 (print) that accepts a variable number
|
||||
of arguments and also provides keywords for specifying the printed
|
||||
separators between the arguments and what should be printed after the
|
||||
las argument."
|
||||
(define printf-quoted (special-lambda (:keys sep end :rest args)
|
||||
(if (nil? args)
|
||||
(begin (print (eval end)) nil)
|
||||
(begin
|
||||
(print (first args))
|
||||
(unless (nil? (rest args))
|
||||
(print (eval sep)))
|
||||
(eval (pair printf-quoted
|
||||
(extend (list :sep (eval sep) :end (eval end)) (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)))
|
||||
|
||||
(eval (pair printf-quoted (extend (list :sep (eval sep) :end (eval end)) args))))
|
||||
(printf-inner args)
|
||||
())
|
||||
|
||||
Reference in New Issue
Block a user