您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

505 行
15 KiB

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