Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

411 righe
11 KiB

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