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.
 
 
 
 
 
 

557 rivejä
16 KiB

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