You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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