Comment detail

分数を小数に展開 (Nested Flatten)

This comment is reply for 182 katsu: schemeですけど、gaucheでしか...(分数を小数に展開). Go to thread root.

副作用無し版。gaucheです。 検索が遅いので、桁が多いと遅くなります。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
(use text.tree)
(use srfi-1)
(use srfi-11)
(use gauche.sequence)

(define (div3 a b)
  (tree->string (cons "0."
    (let loop ((a (* 10 a)) (b b) (r ()) (l ()))
      (cond ((find-index (pa$ = a) l) =>
             (lambda (m)
               (reverse
                 (call-with-values (cut split-at r (+ m 1))
                                   (cut append '("}") <> '("{") <>)))))
            ((< a b) (loop (* a 10) b (cons 0 r) (cons a l)))
            (else (let-values (((quo rem) (quotient&remainder a b)))
                    (if (zero? rem)
                      (reverse (cons quo r))
                      (loop (* rem 10) b (cons quo r) (cons a l))))))))))

Index

Feed

Other

Link

Pathtraq

loading...