25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

74 lines
2.3 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 (obj sequence)
  8. (if (nil? sequence)
  9. (list obj)
  10. (if (nil? (rest sequence))
  11. ))))
  12. (define range
  13. (lambda (:keys from :defaults-to 0 to)
  14. "Returns a sequence of numbers starting with the number
  15. defined by the key 'from' and ends with the number defined in
  16. 'to'."
  17. (if (<= from to)
  18. (pair from (range :from (+ 1 from) :to to))
  19. nil)))
  20. (define map
  21. (lambda (function sequence)
  22. "Takes a sequence and a function as arguments and returns a
  23. new sequence which contains the results of using the first
  24. sequences elemens as argument to that function."
  25. (if (nil? sequence)
  26. sequence
  27. (pair (function (first sequence))
  28. (map (rest sequence) function)))))
  29. (define reduce
  30. (lambda (function sequence)
  31. "Takes a sequence and a function as arguments and applies the
  32. function to the argument sequence. This only works correctly if the
  33. given function accepts a variable amount of parameters. If your
  34. funciton is limited to two arguments, use `reduce-binary' instead."
  35. (eval (pair function sequence))))
  36. (define reduce-binary
  37. (lambda (function sequence)
  38. "Takes a sequence and a function as arguments and applies the
  39. function to the argument sequence. reduce-binary applies the
  40. arguments `pair-wise' which means it works with binary functions
  41. as compared to `reduce'."
  42. (if (nil? (rest sequence))
  43. (first sequence)
  44. (function (first sequence)
  45. (reduce-binary (rest sequence) function)))))
  46. (define filter
  47. (lambda (function sequence)
  48. (if (nil? sequence)
  49. nil
  50. (if (function (first sequence))
  51. (pair (first sequence)
  52. (filter (rest sequence) function))
  53. (filter (rest sequence) function)))))
  54. (define printf
  55. (lambda (:keys sep :defaults-to " " end :defaults-to "\n" :rest args)
  56. (if (and (nil? (first args)) (nil? (rest args)))
  57. (print end)
  58. (prog
  59. (print (first args))
  60. (if (not (nil? (rest args)))
  61. (print sep))
  62. ;; TODO(Felix): later we should use `extend' here:
  63. ;; (eval (extend (quote (printf :sep sep :end end)) (rest args)))
  64. (eval (pair printf (pair :sep (pair sep (pair :end (pair end (rest args)))))))
  65. nil))))