Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

412 rindas
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. if (= 0 (% i 2))
  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) 'if)
  103. `(when ,(first (rest body)) ,(rec (rest (rest body)))))
  104. ((= (first (rest 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-syntax (define-module module-name :keys exports :rest body)
  124. (let ((module-prefix (concat-strings (symbol->string module-name) "::")))
  125. (eval `(begin @body))
  126. (pair 'begin
  127. (map (lambda (orig-export-name)
  128. (let ((export-name (string->symbol
  129. (concat-strings module-prefix
  130. (symbol->string orig-export-name)))))
  131. `(define ,export-name
  132. ,(try (eval orig-export-name)
  133. (error "The module does not contain" orig-export-name)))))
  134. exports))))
  135. (define (null? x)
  136. "Checks if the argument is =nil=."
  137. (= x ()))
  138. (define (type=? obj typ)
  139. "Checks if the argument =obj= is of type =typ="
  140. (= (type obj) typ))
  141. (define (types=? :rest objs)
  142. (define (inner objs)
  143. (if objs
  144. (let ((actual-type (type (first objs)))
  145. (desired-type (first (rest objs))))
  146. (if (= actual-type desired-type)
  147. (inner (rest (rest objs)))
  148. ()))
  149. t))
  150. (inner objs))
  151. (define (assert-types= :rest objs)
  152. (define (inner objs)
  153. (when objs
  154. (let ((actual-type (type (first objs)))
  155. (desired-type (first (rest objs))))
  156. (if (= actual-type desired-type)
  157. (inner (rest (rest objs)))
  158. (error "type missmatch" actual-type desired-type)))))
  159. (inner objs))
  160. (define (number? x)
  161. "Checks if the argument is a number."
  162. (type=? x :number))
  163. (define (symbol? x)
  164. "Checks if the argument is a symbol."
  165. (type=? x :symbol))
  166. (define (keyword? x)
  167. "Checks if the argument is a keyword."
  168. (type=? x :keyword))
  169. (define (pair? x)
  170. "Checks if the argument is a pair."
  171. (type=? x :pair))
  172. (define (string? x)
  173. "Checks if the argument is a string."
  174. (type=? x :string))
  175. (define (lambda? x)
  176. "Checks if the argument is a function."
  177. (type=? x :lambda))
  178. (define (macro? x)
  179. "Checks if the argument is a macro."
  180. (type=? x :macro))
  181. (define (special-lambda? x)
  182. "Checks if the argument is a special-lambda."
  183. (type=? x :dynamic-macro))
  184. (define (built-in-function? x)
  185. "Checks if the argument is a built-in function."
  186. (type=? x :built-in-function))
  187. (define (callable? x)
  188. (or (lambda? x)
  189. (special-lambda? x)
  190. (macro? x)
  191. (built-in-function? x)))
  192. (define (end seq)
  193. "Returns the last pair in the sqeuence.
  194. {{{example_start}}}
  195. (define a (list 1 2 3 4))
  196. (printf (end a))
  197. {{{example_end}}}
  198. "
  199. (if (or (null? seq) (not (pair? (rest seq))))
  200. seq
  201. (end (rest seq))))
  202. (define (last seq)
  203. "Returns the (first) of the last (pair) of the given sequence.
  204. {{{example_start}}}
  205. (define a (list 1 2 3 4))
  206. (printf (last a))
  207. {{{example_end}}}
  208. "
  209. (first (end seq)))
  210. (define (extend seq elem)
  211. "Extends a list with the given element, by putting it in
  212. the (rest) of the last element of the sequence."
  213. (if (pair? seq)
  214. (begin
  215. (define e (end seq))
  216. (mutate e (pair (first e) elem))
  217. seq)
  218. elem))
  219. (define (extend2 seq elem)
  220. "Extends a list with the given element, by putting it in
  221. the (rest) of the last element of the sequence."
  222. (printf "addr of (end seq)" (addr-of (end seq)))
  223. (if (pair? seq)
  224. (let ((e (end seq)))
  225. (printf "addr if e inner" (addr-of e))
  226. (mutate e (pair (first e) elem))
  227. seq))
  228. elem)
  229. (define (append seq elem)
  230. "Appends an element to a sequence, by extendeing the list
  231. with (pair elem nil)."
  232. (extend seq (pair elem ())))
  233. (define (length seq)
  234. "Returns the length of the given sequence."
  235. (if (null? seq)
  236. 0
  237. (+ 1 (length (rest seq)))))
  238. (define (member? elem seq)
  239. (when (pair? seq)
  240. (if (= elem (first seq))
  241. t
  242. (member? elem (rest seq)))))
  243. (define (sublist-starting-at-index seq index)
  244. (cond ((< index 0)
  245. (error "sublist-starting-at-index: index must be positive"))
  246. ((null? seq) ())
  247. ((= 0 index) seq)
  248. (else (sublist-starting-at (rest seq) (- index 1)))))
  249. (define (list-without-index seq index)
  250. (cond ((or (< index 0) (null? seq))
  251. (error "list-remove-index!: index out of range"))
  252. ((= 0 index) (rest seq))
  253. (else (pair (first seq) (list-without-index (rest seq) (- index 1))))))
  254. (define (increment val)
  255. "Adds one to the argument."
  256. (+ val 1))
  257. (define (decrement val)
  258. "Subtracts one from the argument."
  259. (- val 1))
  260. (define (range :keys from :defaults-to 0 to)
  261. "Returns a sequence of numbers starting with the number defined by the
  262. key =from= and ends with the number defined in =to=."
  263. (when (< from to)
  264. (pair from (range :from (+ 1 from) :to to))))
  265. (define (range-while :keys from :defaults-to 0 to)
  266. "Returns a sequence of numbers starting with the number defined
  267. by the key 'from' and ends with the number defined in 'to'."
  268. (define result (list (copy from)))
  269. (define head result)
  270. (mutate from (increment from))
  271. (while (< from to)
  272. (begin
  273. (mutate head (pair (first head) (pair (copy from) nil)))
  274. (define head (rest head))
  275. (mutate from (increment from))))
  276. result)
  277. (define (map fun seq)
  278. "Takes a function and a sequence as arguments and returns a new
  279. sequence which contains the results of using the first sequences
  280. elemens as argument to that function."
  281. (if (null? seq)
  282. seq
  283. (pair (fun (first seq))
  284. (map fun (rest seq)))))
  285. (define (reduce fun seq)
  286. "Takes a function and a sequence as arguments and applies the
  287. function to the argument sequence. This only works correctly if the
  288. given function accepts a variable amount of parameters. If your
  289. funciton is limited to two arguments, use [[=reduce-binary=]]
  290. instead."
  291. (apply fun seq))
  292. (define (reduce-binary fun seq)
  293. "Takes a function and a sequence as arguments and applies the
  294. function to the argument sequence. reduce-binary applies the arguments
  295. *pair-wise* which means it works with binary functions as compared to
  296. [[=reduce=]]."
  297. (if (null? (rest seq))
  298. (first seq)
  299. (fun (first seq)
  300. (reduce-binary fun (rest seq)))))
  301. (define (filter fun seq)
  302. "Takes a function and a sequence as arguments and applies the
  303. function to every value in the sequence. If the result of that
  304. funciton application returns a truthy value, the original value is
  305. added to a list, which in the end is returned."
  306. (when seq
  307. (if (fun (first seq))
  308. (pair (first seq)
  309. (filter fun (rest seq)))
  310. (filter fun (rest seq)))))
  311. (define (zip l1 l2)
  312. (unless (and (null? l1) (null? l2))
  313. (pair (list (first l1) (first l2))
  314. (zip (rest l1) (rest l2)))))
  315. (define (unzip lists)
  316. (when lists
  317. (define (iter lists l1 l2)
  318. (define elem (first lists))
  319. (if elem
  320. (iter (rest lists)
  321. (pair (first elem) l1)
  322. (pair (first (rest elem)) l2))
  323. (list l1 l2)))
  324. (iter lists () ())))
  325. (define (enumerate seq)
  326. (define (enumerate-inner seq next-num)
  327. (when seq
  328. (pair (list (first seq) next-num)
  329. (enumerate-inner (rest seq) (+ 1 next-num)))))
  330. (enumerate-inner seq 0))
  331. (define (printf :keys sep :defaults-to " " end :defaults-to "\n" :rest args)
  332. "A wrapper for the built-in function [[=print=]] that accepts a
  333. variable number of arguments and also provides keywords for specifying
  334. the printed separators (=sep=) between the arguments and what should
  335. be printed after the last argument (=end=)."
  336. (define (printf-inner args)
  337. (if args
  338. (begin
  339. (print (first args))
  340. (when (rest args)
  341. (print sep))
  342. (printf-inner (rest args)))
  343. ; else
  344. (print end)))
  345. (printf-inner args)
  346. ())