Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

229 řádky
6.9 KiB

  1. (define-syntax (when condition :rest body)
  2. "Doc String for 'when'"
  3. `(if ,condition ,(pair begin body) nil))
  4. (define-syntax (unless condition :rest body)
  5. `(if ,condition nil ,(pair begin body)))
  6. (define-syntax (n-times times action)
  7. "Executes action times times."
  8. (define (repeat times elem)
  9. (unless (> 1 times)
  10. (pair elem (repeat (- times 1) elem))))
  11. (pair 'begin (repeat times action)))
  12. ;; (define (fib n))
  13. ;; (define-syntax define (name :rest value)
  14. ;; (print name)
  15. ;; (print (type name))
  16. ;; (if (= (type name) :pair)
  17. ;; (begin
  18. ;; ;; (print `(define ,(first name) ,`(pair lambda (pair (rest name) value))))
  19. ;; ;; (print rest)
  20. ;; (print "\naa\n")
  21. ;; (list 'define (first name) (pair 'lambda (pair (rest name) value))))
  22. ;; (begin
  23. ;; ;; (print (pair 'define (pair name value)))
  24. ;; (print "\nbb\n")
  25. ;; (pair 'define (pair name value)))))
  26. (define-syntax (cond :rest clauses)
  27. (define (rec clauses)
  28. (if (= nil clauses)
  29. nil
  30. (if (= (first (first clauses)) 'else)
  31. (begin
  32. (if (not (= () (rest clauses)))
  33. (error "There are additional clauses after the else clause!")
  34. (pair 'begin (rest (first clauses)))))
  35. (list 'if (first (first clauses))
  36. (pair 'begin (rest (first clauses)))
  37. (rec (rest clauses))))))
  38. (rec clauses))
  39. (define (nil? x)
  40. "Checks if the argument is nil."
  41. (= x nil))
  42. (define (number? x)
  43. "Checks if the argument is a number."
  44. (= (type x) :number))
  45. (define (symbol? x)
  46. "Checks if the argument is a symbol."
  47. (= (type x) :symbol))
  48. (define (keyword? x)
  49. "Checks if the argument is a keyword."
  50. (= (type x) :keyword))
  51. (define (pair? x)
  52. "Checks if the argument is a pair."
  53. (= (type x) :pair))
  54. (define (string? x)
  55. "Checks if the argument is a string."
  56. (= (type x) :string))
  57. (define (lambda? x)
  58. "Checks if the argument is a function."
  59. (= (type x) :dynamic-function))
  60. (define (special-lambda? x)
  61. "Checks if the argument is a macro."
  62. (= (type x) :dynamic-macro))
  63. (define (built-n-function? x)
  64. "Checks if the argument is a built-in function."
  65. (= (type x) :built-in-function))
  66. (define (apply fun seq)
  67. "Applies the funciton to the sequence, as in calls the function with
  68. ithe sequence as arguemens."
  69. (eval (pair fun seq)))
  70. (define (end seq)
  71. "Returns the last pair in the sqeuence."
  72. (if (or (nil? seq) (not (pair? (rest seq))))
  73. seq
  74. (end (rest seq))))
  75. (define (last seq)
  76. "Returns the (first) of the last (pair) of the given sequence."
  77. (first (end seq)))
  78. (define (extend seq elem)
  79. "Extends a list with the given element, by putting it in
  80. the (rest) of the last element of the sequence."
  81. (if (pair? seq)
  82. (begin
  83. (define e (end seq))
  84. (mutate e (pair (first e) elem))
  85. seq)
  86. elem))
  87. (define (append seq elem)
  88. "Appends an element to a sequence, by extendeing the list
  89. with (pair elem nil)."
  90. (extend seq (pair elem nil)))
  91. (define (length seq)
  92. "Returns the length of the given sequence."
  93. (if (nil? seq)
  94. 0
  95. (+ 1 (length (rest seq)))))
  96. (define (increment val)
  97. "Adds one to the argument."
  98. (+ val 1))
  99. (define (decrement val)
  100. "Subtracts one from the argument."
  101. (- val 1))
  102. ;; (defmacro for (@symbol @from @to :rest @for-body)
  103. ;; "Designed to resemble a C style for loop. It takes a symbol as
  104. ;; well as its starting number and end number and executes the
  105. ;; @for-body with the defined symbol for all numbers between @from
  106. ;; to @to, where @to is exclusive."
  107. ;; (if (< (eval @from) (eval @to))
  108. ;; (macro-define @op incr)
  109. ;; (if (> (eval @from) (eval @to))
  110. ;; (macro-define @op decr)
  111. ;; (macro-define @op nil)))
  112. ;; (when @op
  113. ;; (macro-define (eval @symbol) (eval @from))
  114. ;; (eval (pair begin @for-body))
  115. ;; (eval (extend (list for @symbol (@op @from) @to) @for-body))))
  116. (define (range :keys from :defaults-to 0 to)
  117. "Returns a sequence of numbers starting with the number defined
  118. by the key 'from' and ends with the number defined in 'to'."
  119. (when (< from to)
  120. (pair from (range :from (+ 1 from) :to to))))
  121. (define (range-while :keys from :defaults-to 0 to)
  122. "Returns a sequence of numbers starting with the number defined
  123. by the key 'from' and ends with the number defined in 'to'."
  124. (define result (list (copy from)))
  125. (define head result)
  126. (mutate from (increment from))
  127. (while (< from to)
  128. (begin
  129. (mutate head (pair (first head) (pair (copy from) nil)))
  130. (define head (rest head))
  131. (mutate from (increment from))))
  132. result)
  133. (define (map fun seq)
  134. "Takes a function and a sequence as arguments and returns a new
  135. sequence which contains the results of using the first sequences
  136. elemens as argument to that function."
  137. (if (nil? seq)
  138. seq
  139. (pair (fun (first seq))
  140. (map fun (rest seq)))))
  141. (define (reduce fun seq)
  142. "Takes a function and a sequence as arguments and applies the
  143. function to the argument sequence. This only works correctly if
  144. the given function accepts a variable amount of parameters. If
  145. your funciton is limited to two arguments, use `reduce-binary'
  146. instead."
  147. (apply fun seq))
  148. (define (reduce-binary fun seq)
  149. "Takes a function and a sequence as arguments and applies the
  150. function to the argument sequence. reduce-binary applies the
  151. arguments `pair-wise' which means it works with binary functions
  152. as compared to `reduce'."
  153. (if (nil? (rest seq))
  154. (first seq)
  155. (fun (first seq)
  156. (reduce-binary fun (rest seq)))))
  157. (define (filter fun seq)
  158. "Takes a function and a sequence as arguments and applies the
  159. function to every value in the sequence. If the result of that
  160. funciton application returns a truthy value, the original value is
  161. added to a list, which in the end is returned."
  162. (when seq
  163. (if (fun (first seq))
  164. (pair (first seq)
  165. (filter fun (rest seq)))
  166. (filter fun (rest seq)))))
  167. (define (zip l1 l2)
  168. (if (and (nil? l1) (nil? l2))
  169. nil
  170. (pair (list (first l1) (first l2)) (zip (rest l1) (rest l2)))))
  171. (define (enumerate seq)
  172. (define (enumerate-inner seq next-num)
  173. (when seq
  174. (pair (list (first seq) next-num)
  175. (enumerate-inner (rest seq) (+ 1 next-num)))))
  176. (enumerate-inner seq 0))
  177. (define (printf :keys sep :defaults-to " " end :defaults-to "\n" :rest args)
  178. "A wrapper for the built-in (print) that accepts a variable number
  179. of arguments and also provides keywords for specifying the printed
  180. separators between the arguments and what should be printed after the
  181. las argument."
  182. (define printf-quoted (special-lambda (:keys @sep @end :rest @args)
  183. (if (nil? @args)
  184. (begin (print (eval @end)) nil)
  185. (begin
  186. (print (first @args))
  187. (unless (nil? (rest @args))
  188. (print (eval @sep)))
  189. (eval (pair printf-quoted
  190. (extend (list :@sep (eval @sep) :@end (eval @end)) (rest @args))))))))
  191. (eval (pair printf-quoted (extend (list :@sep (eval sep) :@end (eval end)) args))))
  192. (define-syntax (pe expr)
  193. `(printf ',expr "evaluates to" ,expr))