選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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