2010/02/18

collatz unfold

プログラミングGauche

unfold

(use srfi-1)
(define (collatz n)
  (unfold (lambda (s)
            (= s 1))
          (lambda (s)
            s)
          (lambda (s)
            (cond ((even? s)(/ s 2))
                  ((odd? s)(+ (* s 3) 1))
                  (else s)))
          n
          (lambda (s)
            (cons s '()))))
(collatz 10)
; -> (10 5 16 8 4 2 1)







途中経過に対してもcollatz





(use srfi-1)
(define (collatz n)
  (let ((stop (lambda (s)
                (= s 1)))
        (term (lambda (s)
                (cond ((even? s)(/ s 2))
                      ((odd? s)(+ (* s 3) 1))
                      (else s)))))
    (unfold stop
            (lambda (s)
              (let ((v (term s)))
                (if (stop v)
                    s
                    (cons s (collatz v)))))
            term
            n
            (lambda (s)
              (cons s '())))))
(collatz 3)
; -> ((3 (10 (5 (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (5 (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (10 (5 (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (5 (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1)
;; ((3
;; (10
;; (5
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (5
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (10
;; (5
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (5
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)







普通の再帰など





(define (collatz n)
  (cond ((= n 1) n)
        ((even? n)(collatz (/ n 2)))
        ((odd? n)(collatz (+ (* n 3) 1)))
        (else n)))
(collatz 10)
; -> 1
(define (collatz n col)
  (cond ((= n 1)(cons n col))
        ((even? n)(collatz (/ n 2)(cons n col)))
        ((odd? n)(collatz (+ (* n 3) 1)(cons n col)))
        (else col)))
(collatz 10 '())
; -> (1 2 4 8 16 5 10)
;; (unfold p f g seed tail-gen)
;; (if (p seed)
;; (tail-gen seed)
;; (cons (f seed)
;; (unfold p f g (g seed))))
(use srfi-1)
(define (collatz n)
  (unfold (lambda (s)
            (= s 1))
          (lambda (s)
            s)
          (lambda (s)
            (cond ((even? s)(/ s 2))
                  ((odd? s)(+ (* s 3) 1))
                  (else s)))
          n
          (lambda (s)
            (cons s '()))))
(collatz 10)
; -> (10 5 16 8 4 2 1)
(use srfi-1)
(define (collatz n)
  (let ((stop (lambda (s)
                (= s 1)))
        (term (lambda (s)
                (cond ((even? s)(/ s 2))
                      ((odd? s)(+ (* s 3) 1))
                      (else s)))))
    (unfold stop
            (lambda (s)
              (let ((v (term s)))
                (if (stop v)
                    s
                    (cons s (collatz v)))))
            term
            n
            (lambda (s)
              (cons s '())))))
(collatz 3)
; -> ((3 (10 (5 (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (5 (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (10 (5 (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (5 (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1) (16 (8 (4 2 1) 2 1) (4 2 1) 2 1) (8 (4 2 1) 2 1) (4 2 1) 2 1)
;; ((3
;; (10
;; (5
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (5
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (10
;; (5
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (5
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (16
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
;; (8
;; (4 2 1) 2 1)
;; (4 2 1) 2 1)
(for-each (lambda (e)
            (display e)
            (newline))
          (collatz 3))





The Little Schemer, 4th EditionThe Seasoned Schemer

0 件のコメント:

コメントを投稿