Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

93 строки
2.7 KiB

  1. (define nil ())
  2. (define nil?
  3. (lambda (x)
  4. "Checks if the argument is nil."
  5. (= x nil)))
  6. (define append
  7. (lambda (seq elem)
  8. (if (nil? seq)
  9. nil
  10. (if (not (= (type (rest seq)) :pair))
  11. ;; we are on the last element
  12. (mutate seq (pair (first seq) elem))
  13. (append (rest seq) elem)))))
  14. (define last
  15. (lambda (seq)
  16. "Returns the last element of the given sequence."
  17. (if (nil? seq)
  18. seq
  19. (if (not (= (type (rest seq)) :pair))
  20. (first seq)
  21. (last (rest seq))))))
  22. (define n-times
  23. (lambda (times action)
  24. (if (<= times 0)
  25. nil
  26. (prog
  27. (eval action)
  28. (n-times (- 1 times) action)))))
  29. (define range
  30. (lambda (:keys from :defaults-to 0 to)
  31. "Returns a sequence of numbers starting with the number
  32. defined by the key 'from' and ends with the number defined in
  33. 'to'."
  34. (if (<= from to)
  35. (pair from (range :from (+ 1 from) :to to))
  36. nil)))
  37. (define map
  38. (lambda (function sequence)
  39. "Takes a sequence and a function as arguments and returns a
  40. new sequence which contains the results of using the first
  41. sequences elemens as argument to that function."
  42. (if (nil? sequence)
  43. sequence
  44. (pair (function (first sequence))
  45. (map (rest sequence) function)))))
  46. (define reduce
  47. (lambda (function sequence)
  48. "Takes a sequence and a function as arguments and applies the
  49. function to the argument sequence. This only works correctly if the
  50. given function accepts a variable amount of parameters. If your
  51. funciton is limited to two arguments, use `reduce-binary' instead."
  52. (eval (pair function sequence))))
  53. (define reduce-binary
  54. (lambda (function sequence)
  55. "Takes a sequence and a function as arguments and applies the
  56. function to the argument sequence. reduce-binary applies the
  57. arguments `pair-wise' which means it works with binary functions
  58. as compared to `reduce'."
  59. (if (nil? (rest sequence))
  60. (first sequence)
  61. (function (first sequence)
  62. (reduce-binary (rest sequence) function)))))
  63. (define filter
  64. (lambda (function sequence)
  65. (if (nil? sequence)
  66. nil
  67. (if (function (first sequence))
  68. (pair (first sequence)
  69. (filter (rest sequence) function))
  70. (filter (rest sequence) function)))))
  71. (define printf
  72. (lambda (:keys sep :defaults-to " " end :defaults-to "\n" :rest args)
  73. (if (and (nil? (first args)) (nil? (rest args)))
  74. (print end)
  75. (prog
  76. (print (first args))
  77. (if (not (nil? (rest args)))
  78. (print sep))
  79. ;; TODO(Felix): later we should use `extend' here:
  80. ;; (eval (extend (quote (printf :sep sep :end end)) (rest args)))
  81. (eval (extend '(printf :sep sep :end end) (rest args)))
  82. nil))))