Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

406 рядки
11 KiB

  1. (define-syntax (pe expr)
  2. `(printf ',expr "evaluates to" ,expr))
  3. (define-syntax (when condition :rest body)
  4. "Special form for when multiple actions should be done if a
  5. condition is true.
  6. {{{example_start}}}
  7. (when (not ())
  8. (print \"Hello \")
  9. (print \"from \")
  10. (print \"when!\"))
  11. (when ()
  12. (print \"Goodbye \")
  13. (print \"World!\"))
  14. {{{example_end}}}
  15. "
  16. (if (= (rest body) ())
  17. `(if ,condition @body nil)
  18. `(if ,condition (begin @body) nil)))
  19. (define-syntax (unless condition :rest body)
  20. "Special form for when multiple actions should be done if a
  21. condition is false."
  22. (if (= (rest body) ())
  23. `(if ,condition nil @body)
  24. `(if ,condition nil (begin @body))))
  25. (define-syntax (n-times times action)
  26. "Executes action times times."
  27. (define (repeat times elem)
  28. (unless (> 1 times)
  29. (pair elem (repeat (- times 1) elem))))
  30. `(begin @(repeat times action)))
  31. (define-syntax (let bindings :rest body)
  32. (define (unzip lists)
  33. (when lists
  34. (define (iter lists l1 l2)
  35. (define elem (first lists))
  36. (if elem
  37. (iter (rest lists)
  38. (pair (first elem) l1)
  39. (pair (first (rest elem)) l2))
  40. (list l1 l2))))
  41. (iter lists () ()))
  42. (define unzipped (unzip bindings))
  43. `((lambda ,(first unzipped) @body) @(first (rest unzipped))))
  44. (define-syntax (cond :rest clauses)
  45. (define (rec clauses)
  46. (if (= nil clauses)
  47. nil
  48. (if (= (first (first clauses)) 'else)
  49. (begin
  50. (if (not (= (rest clauses) ()))
  51. (error "There are additional clauses after the else clause!")
  52. (pair 'begin (rest (first clauses)))))
  53. `(if ,(first (first clauses))
  54. (begin @(rest (first clauses)))
  55. ,(rec (rest clauses))))))
  56. (rec clauses))
  57. (define-syntax (case var :rest clauses)
  58. (define (rec clauses)
  59. (if (= nil clauses)
  60. nil
  61. (if (= (first (first clauses)) 'else)
  62. (begin
  63. (if (not (= (rest clauses) ()))
  64. (error "There are additional clauses after the else clause!")
  65. (pair 'begin (rest (first clauses)))))
  66. `(if (member? ,var ',(first (first clauses)))
  67. (begin @(rest (first clauses)))
  68. ,(rec (rest clauses))))))
  69. (rec clauses))
  70. (define-syntax (define-special name-and-args :rest body)
  71. `(define ,(first name-and-args) (special-lambda ,(rest name-and-args) @body)))
  72. (define-syntax (construct-list :rest body)
  73. "
  74. {{{example_start}}}
  75. (construct-list
  76. i <- '(1 2 3 4 5)
  77. yield (* i i))
  78. {{{example_end}}}
  79. (construct-list
  80. i <- '(1 2 3 4)
  81. j <- '(A B)
  82. yield (pair i j))
  83. (construct-list
  84. i <- '(1 2 3 4 5 6 7 8)
  85. when (evenp i)
  86. yield i)
  87. "
  88. (define (append-map f ll)
  89. (unless (= ll ())
  90. (define val (f (first ll)))
  91. (if (= (first val) ())
  92. (append-map f (rest ll))
  93. (extend
  94. val
  95. (append-map f (rest ll))))))
  96. (define (rec body)
  97. (cond
  98. ((= () body) ())
  99. ((= () (rest body)) (first body))
  100. ((= (first (rest body)) '<-)
  101. `(,append-map (lambda (,(first body)) (list ,(rec (rest (rest (rest body)))))) ,(first (rest (rest body)))))
  102. ((= (first body) 'when)
  103. `(if ,(first (rest body)) ,(rec (rest (rest body)))))
  104. ((= (first body) 'yield)
  105. (first (rest body)))
  106. (else (error "Not a do-able expression"))))
  107. (rec body))
  108. (define-syntax (apply fun seq)
  109. "Applies the function to the sequence, as in calls the function with
  110. ithe sequence as arguemens."
  111. `(eval (pair ,fun ,seq)))
  112. (define-syntax (define-typed args :rest body)
  113. (define (get-arg-names args)
  114. (when args
  115. (pair (first args)
  116. (get-arg-names (rest (rest args))))))
  117. (let ((name (first args))
  118. (lambda-list (rest args))
  119. (arg-names (get-arg-names (rest args))))
  120. `(define (,name @arg-names)
  121. (assert-types= @lambda-list)
  122. @body)))
  123. (define-typed (ttt a :number b :alist)
  124. (printf a b))
  125. (define-syntax (define-package name :rest body)
  126. `(define ,(string->symbol (concat-strings (symbol->string name) "->"))
  127. ((lambda ()
  128. @body
  129. (set-type
  130. (special-lambda (:rest args)
  131. (let ((op (first args))
  132. (args (rest args)))
  133. (if (callable? (eval op))
  134. (apply op args)
  135. (eval op))))
  136. :package)))))
  137. (define (null? x)
  138. "Checks if the argument is =nil=."
  139. (= x ()))
  140. (define (type=? obj typ)
  141. "Checks if the argument =obj= is of type =typ="
  142. (= (type obj) typ))
  143. (define (types=? :rest objs)
  144. ;; TODO make inner rec functoin to avoid evalutating every time
  145. (if objs
  146. (begin
  147. (assert (keyword? (first (rest objs))))
  148. (if (type=? (first objs) (first (rest objs)))
  149. (apply types=? (rest (rest objs)))
  150. ()))
  151. t))
  152. (define (assert-types= :rest objs)
  153. (break)
  154. (unless (apply types=? objs)
  155. (error "assert-types=: types do not match")))
  156. (define (number? x)
  157. "Checks if the argument is a number."
  158. (type=? x :number))
  159. (define (symbol? x)
  160. "Checks if the argument is a symbol."
  161. (type=? x :symbol))
  162. (define (keyword? x)
  163. "Checks if the argument is a keyword."
  164. (type=? x :keyword))
  165. (define (pair? x)
  166. "Checks if the argument is a pair."
  167. (type=? x :pair))
  168. (define (string? x)
  169. "Checks if the argument is a string."
  170. (type=? x :string))
  171. (define (lambda? x)
  172. "Checks if the argument is a function."
  173. (type=? x :lambda))
  174. (define (macro? x)
  175. "Checks if the argument is a macro."
  176. (type=? x :macro))
  177. (define (special-lambda? x)
  178. "Checks if the argument is a special-lambda."
  179. (type=? x :dynamic-macro))
  180. (define (built-in-function? x)
  181. "Checks if the argument is a built-in function."
  182. (type=? x :built-in-function))
  183. (define (callable? x)
  184. (or (lambda? x)
  185. (special-lambda? x)
  186. (macro? x)
  187. (built-in-function? x)))
  188. (define (end seq)
  189. "Returns the last pair in the sqeuence.
  190. {{{example_start}}}
  191. (define a (list 1 2 3 4))
  192. (printf (end a))
  193. {{{example_end}}}
  194. "
  195. (if (or (null? seq) (not (pair? (rest seq))))
  196. seq
  197. (end (rest seq))))
  198. (define (last seq)
  199. "Returns the (first) of the last (pair) of the given sequence.
  200. {{{example_start}}}
  201. (define a (list 1 2 3 4))
  202. (printf (last a))
  203. {{{example_end}}}
  204. "
  205. (first (end seq)))
  206. (define (extend seq elem)
  207. "Extends a list with the given element, by putting it in
  208. the (rest) of the last element of the sequence."
  209. (if (pair? seq)
  210. (begin
  211. (define e (end seq))
  212. (mutate e (pair (first e) elem))
  213. seq)
  214. elem))
  215. (define (extend2 seq elem)
  216. "Extends a list with the given element, by putting it in
  217. the (rest) of the last element of the sequence."
  218. (printf "addr of (end seq)" (addr-of (end seq)))
  219. (if (pair? seq)
  220. (let ((e (end seq)))
  221. (printf "addr if e inner" (addr-of e))
  222. (mutate e (pair (first e) elem))
  223. seq))
  224. elem)
  225. (define (append seq elem)
  226. "Appends an element to a sequence, by extendeing the list
  227. with (pair elem nil)."
  228. (extend seq (pair elem ())))
  229. (define (length seq)
  230. "Returns the length of the given sequence."
  231. (if (null? seq)
  232. 0
  233. (+ 1 (length (rest seq)))))
  234. (define (member? elem seq)
  235. (when (pair? seq)
  236. (if (= elem (first seq))
  237. t
  238. (member? elem (rest seq)))))
  239. (define (sublist-starting-at-index seq index)
  240. (cond ((< index 0)
  241. (error "sublist-starting-at-index: index must be positive"))
  242. ((null? seq) ())
  243. ((= 0 index) seq)
  244. (else (sublist-starting-at (rest seq) (- index 1)))))
  245. (define (list-without-index seq index)
  246. (cond ((or (< index 0) (null? seq))
  247. (error "list-remove-index!: index out of range"))
  248. ((= 0 index) (rest seq))
  249. (else (pair (first seq) (list-without-index (rest seq) (- index 1))))))
  250. (define (increment val)
  251. "Adds one to the argument."
  252. (+ val 1))
  253. (define (decrement val)
  254. "Subtracts one from the argument."
  255. (- val 1))
  256. (define (range :keys from :defaults-to 0 to)
  257. "Returns a sequence of numbers starting with the number defined by the
  258. key =from= and ends with the number defined in =to=."
  259. (when (< from to)
  260. (pair from (range :from (+ 1 from) :to to))))
  261. (define (range-while :keys from :defaults-to 0 to)
  262. "Returns a sequence of numbers starting with the number defined
  263. by the key 'from' and ends with the number defined in 'to'."
  264. (define result (list (copy from)))
  265. (define head result)
  266. (mutate from (increment from))
  267. (while (< from to)
  268. (begin
  269. (mutate head (pair (first head) (pair (copy from) nil)))
  270. (define head (rest head))
  271. (mutate from (increment from))))
  272. result)
  273. (define (map fun seq)
  274. "Takes a function and a sequence as arguments and returns a new
  275. sequence which contains the results of using the first sequences
  276. elemens as argument to that function."
  277. (if (null? seq)
  278. seq
  279. (pair (fun (first seq))
  280. (map fun (rest seq)))))
  281. (define (reduce fun seq)
  282. "Takes a function and a sequence as arguments and applies the
  283. function to the argument sequence. This only works correctly if the
  284. given function accepts a variable amount of parameters. If your
  285. funciton is limited to two arguments, use [[=reduce-binary=]]
  286. instead."
  287. (apply fun seq))
  288. (define (reduce-binary fun seq)
  289. "Takes a function and a sequence as arguments and applies the
  290. function to the argument sequence. reduce-binary applies the arguments
  291. *pair-wise* which means it works with binary functions as compared to
  292. [[=reduce=]]."
  293. (if (null? (rest seq))
  294. (first seq)
  295. (fun (first seq)
  296. (reduce-binary fun (rest seq)))))
  297. (define (filter fun seq)
  298. "Takes a function and a sequence as arguments and applies the
  299. function to every value in the sequence. If the result of that
  300. funciton application returns a truthy value, the original value is
  301. added to a list, which in the end is returned."
  302. (when seq
  303. (if (fun (first seq))
  304. (pair (first seq)
  305. (filter fun (rest seq)))
  306. (filter fun (rest seq)))))
  307. (define (zip l1 l2)
  308. (unless (and (null? l1) (null? l2))
  309. (pair (list (first l1) (first l2))
  310. (zip (rest l1) (rest l2)))))
  311. (define (unzip lists)
  312. (when lists
  313. (define (iter lists l1 l2)
  314. (define elem (first lists))
  315. (if elem
  316. (iter (rest lists)
  317. (pair (first elem) l1)
  318. (pair (first (rest elem)) l2))
  319. (list l1 l2)))
  320. (iter lists () ())))
  321. (define (enumerate seq)
  322. (define (enumerate-inner seq next-num)
  323. (when seq
  324. (pair (list (first seq) next-num)
  325. (enumerate-inner (rest seq) (+ 1 next-num)))))
  326. (enumerate-inner seq 0))
  327. (define (printf :keys sep :defaults-to " " end :defaults-to "\n" :rest args)
  328. "A wrapper for the built-in function [[=print=]] that accepts a
  329. variable number of arguments and also provides keywords for specifying
  330. the printed separators (=sep=) between the arguments and what should
  331. be printed after the last argument (=end=)."
  332. (define (printf-inner args)
  333. (if args
  334. (begin
  335. (print (first args))
  336. (when (rest args)
  337. (print sep))
  338. (printf-inner (rest args)))
  339. ; else
  340. (print end)))
  341. (printf-inner args)
  342. ())