您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

242 行
7.6 KiB

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