[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/tvysKxmF    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Nov 29:
; $7.11

(define-syntax fold-of
  (syntax-rules (range in is)
    ((_ "z" f b e) (set! b (f b e)))
    ((_ "z" f b e (v range fst pst stp) c ...)
      (let* ((x fst) (p pst) (s stp)
             (le? (if (positive? s) <= >=)))
        (do ((v x (+ v s))) ((le? p v) b)
          (fold-of "z" f b e c ...))))
    ((_ "z" f b e (v range fst pst) c ...)
      (let* ((x fst) (p pst) (s (if (< x p) 1 -1)))
        (fold-of "z" f b e (v range x p s) c ...)))
    ((_ "z" f b e (v range pst) c ...)
      (fold-of "z" f b e (v range 0 pst) c ...))
    ((_ "z" f b e (x in xs) c ...)
      (do ((t xs (cdr t))) ((null? t) b)
        (let ((x (car t)))
          (fold-of "z" f b e c ...))))
    ((_ "z" f b e (x is y) c ...)
      (let ((x y)) (fold-of "z" f b e c ...)))
    ((_ "z" f b e p? c ...)
      (if p? (fold-of "z" f b e c ...)))
    ((_ f i e c ...)
      (let ((b i)) (fold-of "z" f b e c ...)))))

(define-syntax list-of (syntax-rules ()
  ((_ arg ...) (reverse (fold-of
    (lambda (d a) (cons a d)) '() arg ...)))))

(define div711
  (let loop ((d 711) (ds '()))
    (cond ((zero? d) ds)
          ((zero? (modulo 711000000 d))
            (loop (- d 1) (cons d ds)))
          (else (loop (- d 1) ds)))))

(define n 0)

(display
  (list-of (list a b c d)
    (a in div711)
    (b in div711) (<= a b)
    (c in div711) (<= b c)
    (d in div711) (<= c d)
    (q is (begin (set! n (+ n 1)) n))
    (= (+ a b c d) 711)
    (= (* a b c d) 711000000)))

(newline)
(display n)


Output:
1
2
((120 125 150 316))
677040


Create a new paste based on this one


Comments: