選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

224 行
6.9 KiB

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