Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

317 wiersze
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 (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) :dynamic-function))
  117. (define (special-lambda? x)
  118. "Checks if the argument is a macro."
  119. (= (type x) :dynamic-macro))
  120. (define (built-n-function? x)
  121. "Checks if the argument is a built-in function."
  122. (= (type x) :built-in-function))
  123. (define (end seq)
  124. "Returns the last pair in the sqeuence."
  125. (if (or (nil? seq) (not (pair? (rest seq))))
  126. seq
  127. (end (rest seq))))
  128. (define (last seq)
  129. "Returns the (first) of the last (pair) of the given sequence."
  130. (first (end seq)))
  131. (define (extend seq elem)
  132. "Extends a list with the given element, by putting it in
  133. the (rest) of the last element of the sequence."
  134. (if (pair? seq)
  135. (begin
  136. (define e (end seq))
  137. (mutate e (pair (first e) elem))
  138. seq)
  139. elem))
  140. (define (extend2 seq elem)
  141. "Extends a list with the given element, by putting it in
  142. the (rest) of the last element of the sequence."
  143. (printf "addr of (end seq)" (addr-of (end seq)))
  144. (if (pair? seq)
  145. (let ((e (end seq)))
  146. (printf "addr if e inner" (addr-of e))
  147. (mutate e (pair (first e) elem))
  148. seq))
  149. elem)
  150. (define (append seq elem)
  151. "Appends an element to a sequence, by extendeing the list
  152. with (pair elem nil)."
  153. (extend seq (pair elem ())))
  154. (define (length seq)
  155. "Returns the length of the given sequence."
  156. (if (nil? seq)
  157. 0
  158. (+ 1 (length (rest seq)))))
  159. (define (increment val)
  160. "Adds one to the argument."
  161. (+ val 1))
  162. (define (decrement val)
  163. "Subtracts one from the argument."
  164. (- val 1))
  165. ;; (defmacro for (@symbol @from @to :rest @for-body)
  166. ;; "Designed to resemble a C style for loop. It takes a symbol as
  167. ;; well as its starting number and end number and executes the
  168. ;; @for-body with the defined symbol for all numbers between @from
  169. ;; to @to, where @to is exclusive."
  170. ;; (if (< (eval @from) (eval @to))
  171. ;; (macro-define @op incr)
  172. ;; (if (> (eval @from) (eval @to))
  173. ;; (macro-define @op decr)
  174. ;; (macro-define @op nil)))
  175. ;; (when @op
  176. ;; (macro-define (eval @symbol) (eval @from))
  177. ;; (eval (pair begin @for-body))
  178. ;; (eval (extend (list for @symbol (@op @from) @to) @for-body))))
  179. (define (range :keys from :defaults-to 0 to)
  180. "Returns a sequence of numbers starting with the number defined
  181. by the key 'from' and ends with the number defined in 'to'."
  182. (when (< from to)
  183. (pair from (range :from (+ 1 from) :to to))))
  184. (define (range-while :keys from :defaults-to 0 to)
  185. "Returns a sequence of numbers starting with the number defined
  186. by the key 'from' and ends with the number defined in 'to'."
  187. (define result (list (copy from)))
  188. (define head result)
  189. (mutate from (increment from))
  190. (while (< from to)
  191. (begin
  192. (mutate head (pair (first head) (pair (copy from) nil)))
  193. (define head (rest head))
  194. (mutate from (increment from))))
  195. result)
  196. (define (map fun seq)
  197. "Takes a function and a sequence as arguments and returns a new
  198. sequence which contains the results of using the first sequences
  199. elemens as argument to that function."
  200. (if (nil? seq)
  201. seq
  202. (pair (fun (first seq))
  203. (map fun (rest seq)))))
  204. (define (reduce fun seq)
  205. "Takes a function and a sequence as arguments and applies the
  206. function to the argument sequence. This only works correctly if
  207. the given function accepts a variable amount of parameters. If
  208. your funciton is limited to two arguments, use `reduce-binary'
  209. instead."
  210. (apply fun seq))
  211. (define (reduce-binary fun seq)
  212. "Takes a function and a sequence as arguments and applies the
  213. function to the argument sequence. reduce-binary applies the
  214. arguments `pair-wise' which means it works with binary functions
  215. as compared to `reduce'."
  216. (if (nil? (rest seq))
  217. (first seq)
  218. (fun (first seq)
  219. (reduce-binary fun (rest seq)))))
  220. (define (filter fun seq)
  221. "Takes a function and a sequence as arguments and applies the
  222. function to every value in the sequence. If the result of that
  223. funciton application returns a truthy value, the original value is
  224. added to a list, which in the end is returned."
  225. (when seq
  226. (if (fun (first seq))
  227. (pair (first seq)
  228. (filter fun (rest seq)))
  229. (filter fun (rest seq)))))
  230. (define (zip l1 l2)
  231. (unless (and (nil? l1) (nil? l2))
  232. (pair (list (first l1) (first l2))
  233. (zip (rest l1) (rest l2)))))
  234. (define (unzip lists)
  235. (when lists
  236. (define (iter lists l1 l2)
  237. (define elem (first lists))
  238. (if elem
  239. (iter (rest lists)
  240. (pair (first elem) l1)
  241. (pair (first (rest elem)) l2))
  242. (list l1 l2))))
  243. (iter lists () ()))
  244. (define (enumerate seq)
  245. (define (enumerate-inner seq next-num)
  246. (when seq
  247. (pair (list (first seq) next-num)
  248. (enumerate-inner (rest seq) (+ 1 next-num)))))
  249. (enumerate-inner seq 0))
  250. (define (printf :keys sep :defaults-to " " end :defaults-to "\n" :rest args)
  251. "A wrapper for the built-in (print) that accepts a variable number
  252. of arguments and also provides keywords for specifying the printed
  253. separators between the arguments and what should be printed after the
  254. las argument."
  255. (define printf-quoted (special-lambda (:keys sep end :rest args)
  256. (if (nil? args)
  257. (begin (print (eval end)) nil)
  258. (begin
  259. (print (first args))
  260. (unless (nil? (rest args))
  261. (print (eval sep)))
  262. (eval (pair printf-quoted
  263. (extend (list :sep (eval sep) :end (eval end)) (rest args))))))))
  264. (eval (pair printf-quoted (extend (list :sep (eval sep) :end (eval end)) args))))