init.lisp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (import core/prelude ())
  2. (defun gcd (x y)
  3. "Compute the greatest common divisor of X and Y.
  4. ### Example
  5. ```cl
  6. > (gcd 52 32)
  7. out = 4
  8. ```"
  9. (loop [(x (math/abs x))
  10. (y (math/abs y))]
  11. [(= y 0) x]
  12. (recur y (mod x y))))
  13. (defun lcm (x y)
  14. "Compute the lowest common multiple of X and Y.
  15. ### Example
  16. ```cl
  17. > (lcm 52 32)
  18. out = 416
  19. ```"
  20. ;; The floor isn't technically needed, but forces it to be an integer under 5.3
  21. (math/floor (math/abs (/ (* x y) (gcd x y)))))
  22. (defun even? (x)
  23. "Is X an even number?
  24. ### Example
  25. ```cl
  26. > (even? 2)
  27. out = true
  28. > (even? 1)
  29. out = false
  30. ```"
  31. (= (mod x 2) 0))
  32. (defun odd? (x)
  33. "Is X an odd number?
  34. ### Example
  35. ```cl
  36. > (odd? 1)
  37. out = true
  38. > (odd? 2)
  39. out = false
  40. ```"
  41. (/= (mod x 2) 0))
  42. (defun nan? (x)
  43. "Is X equal to NaN?
  44. ### Example
  45. ```cl
  46. > (nan? (/ 0 0))
  47. out = true
  48. > (nan? 1)
  49. out = false
  50. ```"
  51. (/= x x))
  52. (defun succ (x)
  53. "Return the successor of the number X."
  54. (+ x 1))
  55. (defun pred (x)
  56. "Return the predecessor of the number X."
  57. (- x 1))
  58. (defun round (x)
  59. "Round X, to the nearest integer.
  60. ### Example:
  61. ```cl
  62. > (round 1.5)
  63. out = 2
  64. > (round 1.3)
  65. out = 1
  66. > (round -1.3)
  67. out = -1
  68. ```"
  69. (let* [((i f) (math/modf x))]
  70. (if (if (< x 0)
  71. (<= -0.5 f)
  72. (>= f 0.5))
  73. (math/ceil x)
  74. (math/floor x))))
  75. (define tiny "Negative infinity" (* -1 math/huge))