您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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