fork download
  1. ; three simple math problems
  2.  
  3. (define-syntax fold-of
  4. (syntax-rules (range in is)
  5. ((_ "z" f b e) (set! b (f b e)))
  6. ((_ "z" f b e (v range fst pst stp) c ...)
  7. (let* ((x fst) (p pst) (s stp)
  8. (le? (if (positive? s) =)))
  9. (do ((v x (+ v s))) ((le? p v) b)
  10. (fold-of "z" f b e c ...))))
  11. ((_ "z" f b e (v range fst pst) c ...)
  12. (let* ((x fst) (p pst) (s (if (< x p) 1 -1)))
  13. (fold-of "z" f b e (v range x p s) c ...)))
  14. ((_ "z" f b e (v range pst) c ...)
  15. (fold-of "z" f b e (v range 0 pst) c ...))
  16. ((_ "z" f b e (x in xs) c ...)
  17. (do ((t xs (cdr t))) ((null? t) b)
  18. (let ((x (car t)))
  19. (fold-of "z" f b e c ...))))
  20. ((_ "z" f b e (x is y) c ...)
  21. (let ((x y)) (fold-of "z" f b e c ...)))
  22. ((_ "z" f b e p? c ...)
  23. (if p? (fold-of "z" f b e c ...)))
  24. ((_ f i e c ...)
  25. (let ((b i)) (fold-of "z" f b e c ...)))))
  26.  
  27. (define-syntax list-of (syntax-rules ()
  28. ((_ arg ...) (reverse (fold-of
  29. (lambda (d a) (cons a d)) '() arg ...)))))
  30.  
  31. (define (isqrt n)
  32. (if (not (and (positive? n) (integer? n)))
  33. (error 'isqrt "must be positive integer")
  34. (let loop ((x n))
  35. (let ((y (quotient (+ x (quotient n x)) 2)))
  36. (if (< y x) (loop y) x)))))
  37.  
  38. (define (square? n) (let ((s (isqrt n))) (= (* s s) n)))
  39.  
  40. (define (fact n) (if (zero? n) 1 (* n (fact (- n 1)))))
  41.  
  42. (display
  43. (do ((y 10 (+ y 1)))
  44. ((square? (- (expt 2 y) 615))
  45. (list (isqrt (- (expt 2 y) 615)) y))))
  46. (newline)
  47.  
  48. (display
  49. (list-of (list a b c)
  50. (a range 1 10)
  51. (b range 1 10)
  52. (c range 1 10)
  53. (= (+ (* 100 a) (* 10 b) c)
  54. (+ (fact 1) (fact b) (fact c)))))
  55. (newline)
  56.  
  57. (display
  58. (list-of (list p i e)
  59. (p range 1 10)
  60. (i range 1 10)
  61. (e range 1 10)
  62. (= (+ (sqrt (+ (* 10 p) i)) e)
  63. (sqrt (+ (* 100 p) (* 10 i) e))))
  64. )
  65. (newline)
Success #stdin #stdout 0.11s 51152KB
stdin
Standard input is empty
stdout
(59 12)
((1 4 5))
((1 6 9))