You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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