You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

309 lines
9.1 KiB

  1. (define-syntax (pe expr)
  2. `(printf ',expr "evaluates to" ,expr))
  3. (define-syntax (when condition :rest body)
  4. "Doc String for `when'"
  5. (if (= (rest body) ())
  6. `(if ,condition @body)
  7. `(if ,condition (begin @body))))
  8. (define-syntax (unless condition :rest body)
  9. (if (= (rest body) ())
  10. `(if ,condition nil @body)
  11. `(if ,condition nil (begin @body))))
  12. (define-syntax (n-times times action)
  13. "Executes action times times."
  14. (define (repeat times elem)
  15. (unless (> 1 times)
  16. (pair elem (repeat (- times 1) elem))))
  17. `(begin @(repeat times action)))
  18. (define-syntax (let bindings :rest body)
  19. (define (unzip lists)
  20. (when lists
  21. (define (iter lists l1 l2)
  22. (define elem (first lists))
  23. (if elem
  24. (iter (rest lists)
  25. (pair (first elem) l1)
  26. (pair (first (rest elem)) l2))
  27. (list l1 l2))))
  28. (iter lists () ()))
  29. (define unzipped (unzip bindings))
  30. `((lambda ,(first unzipped) @body) @(first (rest unzipped))))
  31. (define-syntax (cond :rest clauses)
  32. (define (rec clauses)
  33. (if (= nil clauses)
  34. nil
  35. (if (= (first (first clauses)) 'else)
  36. (begin
  37. (if (not (= () (rest clauses)))
  38. (error "There are additional clauses after the else clause!")
  39. (pair 'begin (rest (first clauses)))))
  40. `(if ,(first (first clauses))
  41. (begin @(rest (first clauses)))
  42. ,(rec (rest clauses))))))
  43. (rec clauses))
  44. (define-syntax (define-special name-and-args :rest body)
  45. `(define ,(first name-and-args) (special-lambda ,(rest name-and-args) @body)))
  46. (define-syntax (do-list :rest body)
  47. (define (append-map f ll)
  48. (unless (= ll ())
  49. (define val (f (first ll)))
  50. (if (= (first val) ())
  51. (append-map f (rest ll))
  52. (extend
  53. val
  54. (append-map f (rest ll))))))
  55. (define (rec body)
  56. (cond
  57. ((= () body) ())
  58. ((= () (rest body)) (first body))
  59. ((= (first (rest body)) '<-)
  60. `(,append-map (lambda (,(first body)) (list ,(rec (rest (rest (rest body)))))) ,(first (rest (rest body)))))
  61. ((= (first body) 'when)
  62. `(if ,(first (rest body)) ,(rec (rest (rest body)))))
  63. ((= (first body) 'yield)
  64. (first (rest body)))
  65. (t (error "Not a do-able expression: ~S" `(quote ,body))))
  66. )
  67. (rec body))
  68. ;; (define (append-map f ll)
  69. ;; (unless (= ll ())
  70. ;; (define val (f (first ll)))
  71. ;; (if (= (first val) ())
  72. ;; (append-map f (rest ll))
  73. ;; (extend
  74. ;; val
  75. ;; (append-map f (rest ll))))))
  76. ;; (define-special (do-list :rest body)
  77. ;; (cond
  78. ;; ((= () body) ())
  79. ;; ((= () (rest body)) (first body))
  80. ;; ((= (first (rest body)) '<-)
  81. ;; `(,append-map (lambda (,(first body)) (list ,(eval `(do-list @(rest (rest (rest body))))))) ,(first (rest (rest body)))))
  82. ;; ((= (first body) 'when)
  83. ;; `(if ,(first (rest body)) ,(eval `(do-list @(rest (rest body))))))
  84. ;; ((= (first body) 'yield)
  85. ;; (first (rest body)))
  86. ;; (t (error "Not a do-able expression: ~S" `(quote ,body))))
  87. ;; )
  88. (define (nil? x)
  89. "Checks if the argument is nil."
  90. (= x nil))
  91. (define (number? x)
  92. "Checks if the argument is a number."
  93. (= (type x) :number))
  94. (define (symbol? x)
  95. "Checks if the argument is a symbol."
  96. (= (type x) :symbol))
  97. (define (keyword? x)
  98. "Checks if the argument is a keyword."
  99. (= (type x) :keyword))
  100. (define (pair? x)
  101. "Checks if the argument is a pair."
  102. (= (type x) :pair))
  103. (define (string? x)
  104. "Checks if the argument is a string."
  105. (= (type x) :string))
  106. (define (lambda? x)
  107. "Checks if the argument is a function."
  108. (= (type x) :dynamic-function))
  109. (define (special-lambda? x)
  110. "Checks if the argument is a macro."
  111. (= (type x) :dynamic-macro))
  112. (define (built-n-function? x)
  113. "Checks if the argument is a built-in function."
  114. (= (type x) :built-in-function))
  115. (define (apply fun seq)
  116. "Applies the funciton to the sequence, as in calls the function with
  117. ithe sequence as arguemens."
  118. (eval (pair fun seq)))
  119. (define (end seq)
  120. "Returns the last pair in the sqeuence."
  121. (if (or (nil? seq) (not (pair? (rest seq))))
  122. seq
  123. (end (rest seq))))
  124. (define (last seq)
  125. "Returns the (first) of the last (pair) of the given sequence."
  126. (first (end seq)))
  127. (define (extend seq elem)
  128. "Extends a list with the given element, by putting it in
  129. the (rest) of the last element of the sequence."
  130. (if (pair? seq)
  131. (begin
  132. (define e (end seq))
  133. (mutate e (pair (first e) elem))
  134. seq)
  135. elem))
  136. (define (extend2 seq elem)
  137. "Extends a list with the given element, by putting it in
  138. the (rest) of the last element of the sequence."
  139. (printf "addr of (end seq)" (addr-of (end seq)))
  140. (if (pair? seq)
  141. (let ((e (end seq)))
  142. (printf "addr if e inner" (addr-of e))
  143. (mutate e (pair (first e) elem))
  144. seq))
  145. elem)
  146. (define (append seq elem)
  147. "Appends an element to a sequence, by extendeing the list
  148. with (pair elem nil)."
  149. (extend seq (pair elem ())))
  150. (define (length seq)
  151. "Returns the length of the given sequence."
  152. (if (nil? seq)
  153. 0
  154. (+ 1 (length (rest seq)))))
  155. (define (increment val)
  156. "Adds one to the argument."
  157. (+ val 1))
  158. (define (decrement val)
  159. "Subtracts one from the argument."
  160. (- val 1))
  161. ;; (defmacro for (@symbol @from @to :rest @for-body)
  162. ;; "Designed to resemble a C style for loop. It takes a symbol as
  163. ;; well as its starting number and end number and executes the
  164. ;; @for-body with the defined symbol for all numbers between @from
  165. ;; to @to, where @to is exclusive."
  166. ;; (if (< (eval @from) (eval @to))
  167. ;; (macro-define @op incr)
  168. ;; (if (> (eval @from) (eval @to))
  169. ;; (macro-define @op decr)
  170. ;; (macro-define @op nil)))
  171. ;; (when @op
  172. ;; (macro-define (eval @symbol) (eval @from))
  173. ;; (eval (pair begin @for-body))
  174. ;; (eval (extend (list for @symbol (@op @from) @to) @for-body))))
  175. (define (range :keys from :defaults-to 0 to)
  176. "Returns a sequence of numbers starting with the number defined
  177. by the key 'from' and ends with the number defined in 'to'."
  178. (when (< from to)
  179. (pair from (range :from (+ 1 from) :to to))))
  180. (define (range-while :keys from :defaults-to 0 to)
  181. "Returns a sequence of numbers starting with the number defined
  182. by the key 'from' and ends with the number defined in 'to'."
  183. (define result (list (copy from)))
  184. (define head result)
  185. (mutate from (increment from))
  186. (while (< from to)
  187. (begin
  188. (mutate head (pair (first head) (pair (copy from) nil)))
  189. (define head (rest head))
  190. (mutate from (increment from))))
  191. result)
  192. (define (map fun seq)
  193. "Takes a function and a sequence as arguments and returns a new
  194. sequence which contains the results of using the first sequences
  195. elemens as argument to that function."
  196. (if (nil? seq)
  197. seq
  198. (pair (fun (first seq))
  199. (map fun (rest seq)))))
  200. (define (reduce fun seq)
  201. "Takes a function and a sequence as arguments and applies the
  202. function to the argument sequence. This only works correctly if
  203. the given function accepts a variable amount of parameters. If
  204. your funciton is limited to two arguments, use `reduce-binary'
  205. instead."
  206. (apply fun seq))
  207. (define (reduce-binary fun seq)
  208. "Takes a function and a sequence as arguments and applies the
  209. function to the argument sequence. reduce-binary applies the
  210. arguments `pair-wise' which means it works with binary functions
  211. as compared to `reduce'."
  212. (if (nil? (rest seq))
  213. (first seq)
  214. (fun (first seq)
  215. (reduce-binary fun (rest seq)))))
  216. (define (filter fun seq)
  217. "Takes a function and a sequence as arguments and applies the
  218. function to every value in the sequence. If the result of that
  219. funciton application returns a truthy value, the original value is
  220. added to a list, which in the end is returned."
  221. (when seq
  222. (if (fun (first seq))
  223. (pair (first seq)
  224. (filter fun (rest seq)))
  225. (filter fun (rest seq)))))
  226. (define (zip l1 l2)
  227. (unless (and (nil? l1) (nil? l2))
  228. (pair (list (first l1) (first l2))
  229. (zip (rest l1) (rest l2)))))
  230. (define (unzip lists)
  231. (when lists
  232. (define (iter lists l1 l2)
  233. (define elem (first lists))
  234. (if elem
  235. (iter (rest lists)
  236. (pair (first elem) l1)
  237. (pair (first (rest elem)) l2))
  238. (list l1 l2))))
  239. (iter lists () ()))
  240. (define (enumerate seq)
  241. (define (enumerate-inner seq next-num)
  242. (when seq
  243. (pair (list (first seq) next-num)
  244. (enumerate-inner (rest seq) (+ 1 next-num)))))
  245. (enumerate-inner seq 0))
  246. (define (printf :keys sep :defaults-to " " end :defaults-to "\n" :rest args)
  247. "A wrapper for the built-in (print) that accepts a variable number
  248. of arguments and also provides keywords for specifying the printed
  249. separators between the arguments and what should be printed after the
  250. las argument."
  251. (define printf-quoted (special-lambda (:keys sep end :rest args)
  252. (if (nil? args)
  253. (begin (print (eval end)) nil)
  254. (begin
  255. (print (first args))
  256. (unless (nil? (rest args))
  257. (print (eval sep)))
  258. (eval (pair printf-quoted
  259. (extend (list :sep (eval sep) :end (eval end)) (rest args))))))))
  260. (eval (pair printf-quoted (extend (list :sep (eval sep) :end (eval end)) args))))