Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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