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ů.
 
 
 
 
 
 

198 řádky
6.4 KiB

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