Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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