25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

170 satır
4.6 KiB

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