25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

441 satır
12 KiB

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