277.scheme 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/guile \
  2. -e main -s
  3. !#
  4. (define (main args)
  5. ;;(display (get-type args))
  6. (let loop ((args args))
  7. (unless (null? (cdr args))
  8. (let ((num (string->number (car (cdr args))))
  9. (den (string->number (car (cdr (cdr args))))))
  10. ;; (display (car (cdr args)))
  11. ;; (display " ")
  12. ;; (display (car (cdr (cdr args))))
  13. ;; (display "\n")
  14. (simplyify num den)
  15. (loop (cdr (cdr args)))))))
  16. (define (simplyify num den)
  17. (let loop ((factor (commonFactor num den)))
  18. (if (number? factor)
  19. (begin
  20. (set! num (/ num factor))
  21. (set! den (/ den factor))
  22. (loop (commonFactor num den)))
  23. ;;(cons num (cons den '()))
  24. (begin
  25. (display num)
  26. (display " ")
  27. (display den)
  28. (display "\n")))))
  29. (define (commonFactor a b)
  30. ;;the named loop defined a closure. It's essentially naming a function called loop, and calling it repeatily
  31. (let loop ((factor 2))
  32. ;; if facter is half of the number, then return #f
  33. (cond ((= factor (1+ (ceiling (/ a 2))))
  34. ;; if factor is half of a, then a is prime.
  35. ;; test to see if a is a factor of b, and a != b
  36. ;; Also a != 1... I feel like this is not very mathematical
  37. (if (and (not (= a b)) (= 0 (modulo b a)) (not (= a 1)))
  38. a
  39. #f))
  40. ((= 0 (modulo a factor) (modulo b factor))
  41. factor)
  42. ;;if factor does not evenly divide a and b, then start the loop again
  43. (else (loop (1+ factor))))))