Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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