[scheme][Gauche][DrScheme]組み合わせ(combination)
(define combination (lambda (r l) (cond ((null? l) '()) ((or (zero? r)(> r (length l))) '()) ((= r 1)(map list l)) ((= r (length l))(list l)) (else (append (map (lambda (n)(cons (car l) n)) (combination (- r 1)(cdr l))) (combination r (cdr l))))))) |
実行結果
(combination 3 '(a b c)) ;; => ((a b c)) (combination 3 '(a b c d)) ;; => ((a b c) (a b d) (a . #0=(c d)) (b . #0#)) (combination 5 '(a b c d e f g)) ;; => ((a b c) (a b d) (a . #0=(c d)) (b . #0#)) gosh> ((a b c d e) (a b c d f) (a b c d g) (a b c e f) (a b c e g) (a b c . #0=(f g)) (a b d e f) (a b d e g) (a b d . #0#) (a b . #1=(e . #0#)) (a c d e f) (a c d e g) (a c d . #0#) (a c . #1#) (a . #2=(d . #1#)) (b c d e f) (b c d e g) (b c d . #0#) (b c . #1#) (b . #2#) (c . #2#)) (combination 3 '(a b c d e f g)) ;; => ((a b c) (a b d) (a b e) (a b f) (a b g) (a c d) (a c e) (a c f) (a c g) (a d e) (a d f) (a d g) (a e f) (a e g) (a . #0=(f g)) (b c d) (b c e) (b c f) (b c g) (b d e) (b d f) (b d g) (b e f) (b e g) (b . #0#) (c d e) (c d f) (c d g) (c e f) (c e g) (c . #0#) (d e f) (d e g) (d . #0#) (e . #0#)) |
あってる?
0 件のコメント:
コメントを投稿