(続々)Gaucheで「カブらない数字を4桁だすの」をやる*1
http://d.hatena.ne.jp/n9d/20071120/1195568232
で、shiroさんに教えて頂いたのでもう少しやってみる。
gauche.sequenceを使う方法
>> (begin (use srfi-1) (use gauche.sequence) (take (shuffle (iota 10)) 4)) => (8 9 5 0)
append-mapを使う方法
やばい、今度は引数の勉強しなきゃいけなくなった。
>> (use util.combinations)(use srfi-27) => #<undef> >> => #<undef> >> (define permutations2 (lambda (x y) (append-map permutations (combinations x y)))) => permutations2 >> (let ((a (permutations2 (iota 10) 4))) (list-ref a (random-integer (length a)))) => (6 2 0 5)
可変引数に対応したバージョンのpermutationsをつくる
>> (use util.combinations) => #<undef> >> (define permutations.super permutations) => permutations.super >> (define permutations (lambda (x . y) (if (null? y) (permutations.super x) (append-map permutations.super (combinations x (car y)))))) => permutations >> (permutations '(1 2 3)) => ((1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1)) >> (permutations '(1 2 3) 2) => ((1 2) (2 1) (1 3) (3 1) (2 3) (3 2))