cube-root.scm 965 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/home/joshua/.guix-profile/bin/guile \
  2. -e main -s
  3. !#
  4. (define (main args)
  5. (define number (car (cdr args)))
  6. (display (cube (string->number number) (string->number number)))
  7. (display "\n"))
  8. ;;Implementing a cube root procedure
  9. ;; This uses a recursive function to do so.
  10. ;; ((x / (y * y)) + 2y) / 3
  11. ;; This function is really SLOW. Calculating the cube root of 1000, takes FOREVER.
  12. ;; It tries to aproximate the cube
  13. (define (cube number apr)
  14. ;; The estimate has to be within .001 of the actual number.
  15. (define (good-enough?)
  16. (< (abs (- number (* apr apr apr))) 0.001))
  17. (define (improve-apr)
  18. (set! apr (/
  19. (+
  20. (* 2 apr)
  21. (/ number
  22. (* apr apr)))
  23. 3)))
  24. (improve-apr)
  25. (if (good-enough?)
  26. (exact->inexact apr)
  27. (cube number apr)))
  28. (define (A x y)
  29. (cond ((= y 0) 0)
  30. ((= x 0 ) (* 2 y))
  31. (else (A (- x 1)
  32. (A x (- y 1))))))