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.
 
 
 
 
 
 

184 Zeilen
5.5 KiB

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