͂߂
͂ƕ֗Ȋ`ŏЉ܂Bl^ P-99: Ninety-Nine Prolog Problems łBٍ͐̃y[W Prolog Programming Yet Anotehr Prolog Problems ƓłA炸܂B
P
Xg̗vfЂƂׂq single? `ĂB
gosh> (single? '(a)) #t gosh> (single? '(a b)) #f gosh> (single? '()) #f
Q
Xg̗vf邩ׂq double? `ĂB
gosh> (double? '(a b)) #t gosh> (double? '(a b c)) #f gosh> (double? '(a)) #f
R
Xg xs ̓Xg ys ׂq longer? xs ys `ĂB
gosh> (longer? '(a b c) '(a b)) #t gosh> (longer? '(a b) '(a b)) #f gosh> (longer? '(a) '(a b)) #f
S
Xg xs ̍Ō߂ last ƁAŌ̗vf菜 butlast `ĂB
gosh> (last '(a b c)) (c) gosh> (last '(a)) (a) gosh> (butlast '(a b c)) (a b) gosh> (butlast '(a)) ()
T
Xg xs ̐擪 n ̗vfo take xs n `ĂB
gosh> (take '(a b c d e) 3) (a b c) gosh> (take '(a b c d e) 0) () gosh> (take '(a b c d e) 6) (a b c d e)
U
Xg xs ̐擪 n ̗vf菜 drop xs n `ĂB
gosh> (drop '(a b c d e) 3) (d e) gosh> (drop '(a b c d e) 0) (a b c d e) gosh> (drop '(a b c d e) 6) ()
V
Xg xs n Ԗڂ m - 1 Ԗڂ܂ł̗vfXgƂĎo subseq xs n m `ĂBȂAXg̗vf 0 琔n߂̂Ƃ܂B
gosh> (subseq '(a b c d e) 2 4) (c d) gosh> (subseq '(a b c d e) 0 5) (a b c d e) gosh> (subseq '(a b c d e) 0 0) ()
W
Xg xs ̖ n ̗vf菜 butlastn xs n `ĂB
gosh> (butlastn '(a b c d e) 3) (a b) gosh> (butlastn '(a b c d e) 0) (a b c d e) gosh> (butlastn '(a b c d e) 5) ()
X
Xg xs n ̕Xgɕ group xs n `ĂB
gosh> (group '(a b c d e f) 2) ((a b) (c d) (e f)) gosh> (group '(a b c d e f) 3) ((a b c) (d e f)) gosh> (group '(a b c d e f) 4) ((a b c d) (e f))
10
Xg ls ̒ x Ɠvf̈ʒu n ߂ position x ls `ĂBȂAXg̗vf 0 琔n߂̂Ƃ܂B
gosh> (position 'a '(a b c d e)) 0 gosh> (position 'c '(a b c d e)) 2 gosh> (position 'e '(a b c d e)) 4 gosh> (position 'f '(a b c d e)) #f
11
Xg ls x Ɠvf̌ n ߂ count x ls `ĂB
gosh> (count 'a '(a b a b c a b c d)) 3 gosh> (count 'c '(a b a b c a b c d)) 2 gosh> (count 'd '(a b a b c a b c d)) 1 gosh> (count 'e '(a b a b c a b c d)) 0
12
Xg̗vf̍vl߂ sum-list `ĂB
gosh> (sum-list '(1 2 3 4 5)) 15
13
Xg̒ől߂ max-list ƍŏl߂ min-list `ĂB
gosh> (max-list '(5 6 4 7 3 8 2 9 1)) 9 gosh> (min-list '(5 6 4 7 3 8 2 9 1)) 1
14
Xg ls ̒ŗvf x ̉Eׂɗvf y 邩`FbNq adjacent? x y ls `ĂB
gosh> (adjacent? 'a 'b '(a b c d e f)) #t gosh> (adjacent? 'e 'f '(a b c d e f)) #t gosh> (adjacent? 'f 'e '(a b c d e f)) #f
15
Xg ls ̒ŗvf x vf y OɏoĂ邩ׂq before? x y ls `ĂB
gosh> (before? 'a 'b '(a b c d e f)) (b c d e f) gosh> (before? 'c 'b '(a b c d e f)) #f
16
n m ܂łi[Xg iota n m `ĂB
gosh> (iota 1 5) (1 2 3 4 5)
17
Xgdvf菜ďW set-of-list `ĂB
gosh> (set-of-list '(a b c d e f a b c)) (d e f a b c)
18
2 ̏W̘a߂ union `ĂB
gosh> (union '(a b c d) '(c d e f)) (a b c d e f)
19
2 ̏W̐ς߂ intersection `ĂB
gosh> (intersection '(a b c d) '(c d e f)) (c d)
20
2 ̏W̍߂ difference `ĂB
警察航空ユニットは、それをworhtされ
gosh> (difference '(a b c d) '(c d e f)) (a b)
21
2 ̃¥[gς݂̃XgЂƂ̃¥[gς݂̃Xgɂ܂Ƃ߂ merge-list `ĂB
gosh> (merge-list < '(1 3 5 7) '(2 4 6 8)) (1 2 3 4 5 6 7 8)
22
merge-list găXg¥[g merge-sort `ĂB
gosh> (merge-sort < 9 '(5 6 4 7 8 3 2 9 1 10)) (1 2 3 4 5 6 7 8 9) gosh> (merge-sort < 10 '(5 6 4 7 8 3 2 9 1 10)) (1 2 3 4 5 6 7 8 9 10) gosh> (merge-sort < 11 '(5 6 4 7 8 3 2 9 1 10 0)) (0 1 2 3 4 5 6 7 8 9 10)
23
Xg ps Xg ls ́uړ (prefix) v肷 prefix ls ps `ĂBړƂ́A̐擪炠ʒu܂ł̂̕ƂłBXg [a, b, c, d] ̐ړ [ ], [a], [a, b], [a, b, c], [a, b, c, d] 5 ɂȂ܂B
gosh> (prefix '(a b c d e f) '(a b c)) #t gosh> (prefix '(a b c d e f) '(a b c e)) #f gosh> (prefix '(a b c d e f) '()) #t
24
Xg ss Xg ls ́uڔ (suffix) v肷 suffix ls ss `ĂBڔƂ́Âʒu疖܂ł̂̕ƂłBXg [a, b, c, d] ̐ڔ [a, b, c, d], [b, c, d], [c, d], [d], [ ] 5 ɂȂ܂B
gosh> (suffix '(a b c d e f) '(d e f)) #t gosh> (suffix '(a b c d e f) '()) #t gosh> (suffix '(a b c d e f) '(f g)) #f
25
Xg xs Xg ls ̕Xg肷 sublist xs ls `ĂB
gosh> (sublist '(c d e) '(a b c d e f)) #t gosh> (sublist '(d e) '(a b c d e f)) #t gosh> (sublist '(d e g) '(a b c d e f)) #f gosh> (sublist '() '(a b c d e f)) #t
P
Xg : vfЂƂ (define (single? ls) (and (pair? ls) (null? (cdr ls))))
Scheme ̏ꍇA ls XgŁA (cdr ls) XgłÃXg̗vf͈ȂƂ킩܂Blength ŃXg̒߂Kv͂܂B
Q
Xg : vf邩 (define (double? ls) (and (pair? ls) (single? (cdr ls))))
Scheme ̏ꍇAq pair? ^łXgɈȏ̗vf邱Ƃ킩܂BƂ (cdr ls) vfȂƂq single? ŊmF܂Blength ŃXg̒߂Kv͂܂B
R
Xg : Xg xs ys (define (longer? xs ys) (cond ((null? xs) #f) ((null? ys) #t) (else (longer? (cdr xs) (cdr ys))))) ; ʉ (define (longer? xs ys) (and (pair? xs) (or (null? ys) (longer? (cdr xs) (cdr ys)))))
Xg̐擪珇ԂɂǂAr ys XgɂȂ ys ̕Ƃ킩܂Blength ŃXg̒߂ĔrÃvO̕IƎv܂B
S
Xg : Xg̍Ō߂ (define (last ls) (if (null? (cdr ls)) ls (last (cdr ls))))
last ͒PȍċA`ŃXg̍Ō߂Ă܂BGauche ɂ͓ last-pair ܂B
Xg : Ō̗vf菜 (define (butlast ls) (if (single? ls) '() (cons (car ls) (butlast (cdr ls))))) ; ʉ (define (butlast ls) (let loop ((ls ls) (a '())) (if (single? ls) (reverse! a) (loop (cdr ls) (cons (car ls) a)))))
butlast ͈̃Xg ls ̗vfɂȂ܂ōċAĂяo܂BvfɂȂXgԂ܂BƂ́AċAĂяo̕Ԃl cons ŗvfljĂłB
ʉ named let ɂ閖ċAo[WłBݐϕϐ a ɗvfi[A ls ̗vfЂƂɂȂȂAreverse! ŃXg a jIɔ]ĕԂ܂Breverse gIłB
T
Xg : Xg̐擪 n ̗vfo (define (take ls n) (if (or (<= n 0) (null? ls)) '() (cons (car ls) (take (cdr ls) (- n 1))))) ; ʉ (define (take ls n) (let loop ((ls ls) (n n) (a '())) (if (or (<= n 0) (null? ls)) (reverse! a) (loop (cdr ls) (- n 1) (cons (car ls) a)))))
n 0 ȉ܂͈ ls Xg̏ꍇ͋XgԂ܂BłȂ take ċAĂяoāA̕ԂlɃXg̐擪vf (car ls) lj܂Bʉ named let ɂ閖ċAo[WłBݐϕϐ a ɗvfi[āAn 0 ȉ܂ ls XgɂȂAreverse! ŃXg a jIɔ]ĕԂ܂B
ȂAtake Scheme ̃Cu SRFI-1 ɗpӂĂ܂B
2トラップは、RDを下回る。
U
Xg : Xg̐擪 n ̗vf폜 (define (drop ls n) (if (or (<= n 0) (null? ls)) ls (drop (cdr ls) (- n 1))))
drop ͊ȒPłB n 0 ȉ܂͈ ls XgɂȂ܂ drop ċAĂяo邾łBȂAdrop Scheme ̃Cu SRFI-1 ɗpӂĂ܂B
V
Xg : Xgo (define (subseq ls s e) (take (drop ls s) (- e s)))
subseq drop take gƊȒPłBdrop ls s ̗vf菜ÃXg e - s ̗vf take ŎołB
W
Xg : Xg̖ n ̗vf菜 (define (butlastn ls n) (take ls (- (length ls) n)))
Xg ls ̒ m ƂƁAXg̖ n ̗vf菜Ƃ́AXg̐擪 m - n ̗vfoƂƓɂȂ܂Bovf̌ (- (length ls) n)) ŋ߂ take ŗvfo܂B
X
Xg : Xg̕ (define (group ls n) (if (null? ls) '() (cons (take ls n) (group (drop ls n) n)))) ; ʉ (define (group ls n) (let loop ((ls ls) (a '())) (if (null? ls) (reverse! a) (loop (drop ls n) (cons (take ls n) a)))))
group take drop gƊȒPɒ`ł܂Bls Xg̏ꍇ͕łȂ̂ŋXgԂ܂BꂪċA̒‾ɂȂ܂Bls XgłȂꍇA܂ take n ̗vfi[Xg߂܂BɁAn ̗vf菜Xg drop ŋ߂ group ċAĂяo܂B̕Ԃl take ŎoXg cons Œlj킯łB
ʉ named let ɂ閖ċAo[WłBtake ŎoXgݐϕϐ a Ɋi[Als XgɂȂ reverse! ŃXg a jIɔ]ĕԂ܂B
10
Xg : vf̈ʒu߂ (define (position x ls) (let loop ((ls ls) (n 0)) (cond ((null? ls) #f) ((eqv? (car ls) x) n) (else (loop (cdr ls) (+ n 1))))))
named let ̈ n vf̈ʒu¥܂Bls Xg̏ꍇAx Ɠvf͌Ȃ̂ #f Ԃ܂BłȂAq eqv? ŃXg̐擪vf (car ls) x r܂Bꍇ n Ԃ܂BȂꍇ loop ċAĂяoĎ̗vfׂ܂B
11
Xg : vf̌߂ (define (count x ls) (let loop ((ls ls) (n 0)) (cond ((null? ls) n) ((eqv? (car ls) x) (loop (cdr ls) (+ n 1))) (else (loop (cdr ls) n))))) ; ʉ (define (count x ls) (fold (lambda (y n) (if (eqv? x y) (+ n 1) n)) 0 ls))
named let ŃXg̗vfԂɒׁAx ƓvfΗݐϕϐ n ̒l +1 ܂Bʉ͏ݍ݂s fold go[WłBfold Gauche Œ`Ă̂g܂B
12
Xg : vf̍vl߂ (define (sum-list ls) (let loop ((ls ls) (sum 0)) (if (null? ls) sum (loop (cdr ls) (+ (car ls) sum))))) ; ʉ (define (sum-list ls) (fold (lambda (x sum) (+ x sum)) 0 ls))
sum-list named let ŃXg̗vfݐϕϐ sum ɉZ邾łBʉ͏ݍ݂s fold go[WłB
13
Xg : Xgőlƍŏl߂ (define (max-list ls) (let loop ((ls (cdr ls)) (a (car ls))) (cond ((null? ls) a) ((< a (car ls)) (loop (cdr ls) (car ls))) (else (loop (cdr ls) a))))) (define (min-list ls) (let loop ((ls (cdr ls)) (a (car ls))) (cond ((null? ls) a) ((> a (car ls)) (loop (cdr ls) (car ls))) (else (loop (cdr ls) a))))) ; ʉ (define (max-list ls) (fold (lambda (x a) (if (< a x) x a)) (car ls) (cdr ls))) (define (min-list ls) (fold (lambda (x a) (if (> a x) x a)) (car ls) (cdr ls)))
max_list min_list named let ŃvOĂ܂Bݐϕϐ a ɃXg ls ̐擪vfZbg܂BƂ́Ac̗vfԂɒׁAa 傫 () vfA̒l a ɃZbg܂Bʉ fold go[WłB
WAは、リストを呼び出すことはありません
14
Xg : x y ׂ͗荇Ă邩 (define (adjacent x y ls) (if (and (pair? ls) (pair? (cdr ls))) (if (and (eqv? (car ls) x) (eqv? (cadr ls) y)) #t (adjacent x y (cdr ls))) #f)) ; ʉ (define (adjacent x y ls) (let ((xs (memv x ls))) (if xs (if (eqv? (cadr xs) y) #t (adjacent x y (cdr xs))) #f)))
ŏɃXg̗vfȏ゠邱Ƃ`FbN܂BɁA擪̗vf x ƓāA̗vf y ƓƂ`FbN܂Bł #t ԂAłȂ adjacent ċAĂяoāA̗vfׂ܂Bʉ͊ memv go[WłB
15
Xg : x y OɏoĂ邩 (define (before x y ls) (let ((xs (memv x ls))) (if xs (memv y (cdr xs)) #f)))
before ͊ memv gƊȒPɃvO邱Ƃł܂Bls x memv ŒT܂Bx ꍇAxs ̐擪vf x ɂȂ܂B菜Xg memv y T킯łB
16
Xg : ̐ (define (iota n m) (if (> n m) '() (cons n (iota (+ n 1) m)))) ; ʉ (define (iota n m) (let loop ((m m) (a '())) (if (< m n) a (loop (- m 1) (cons m a)))))
iota ͊ȒPłBn m 傫ꍇ͋XgɂȂ܂Bn m ȉ̏ꍇAiota ċAĂяo n + 1 m ܂ł̃XgA̐擪 n lj邾łBʉ named let go[WłB̏ꍇA납琔lĂ邱ƂɒӂĂBm n ȂȂXg a Ԃ܂B
17
Xg : W̐ (define (set-of-list ls) (cond ((null? ls) '()) ((memv (car ls) (cdr ls)) (set-of-list (cdr ls))) (else (cons (car ls) (set-of-list (cdr ls)))))) ; ʉ (define (set-of-list ls) (let loop ((ls ls) (a '())) (cond ((null? ls) (reverse! a)) ((memv (car ls) (cdr ls)) (loop (cdr ls) a)) (else (loop (cdr ls) (cons (car ls) a))))))
q set-of-list ̓Xgdvf菜܂BXg͏dvfȂ̂ŋXĝ܂܂łB̐߂ŁAXg̐擪vf (car ls) c̃Xg (cdr ls) ɂ邩 memv ŒׁAvfΏWɉ܂Belse ߂œvfȂꍇ͂Wɉ܂Bʉ named let gċAo[WłB
18
Xg : W̘a (define (union xs ys) (cond ((null? xs) ys) ((memv (car xs) ys) (union (cdr xs) ys)) (else (cons (car xs) (union (cdr xs) ys))))) ; ʉ 1 (define (union xs ys) (let loop ((xs xs) (a ys)) (cond ((null? xs) a) ((memv (car xs) ys) (loop (cdr xs) a)) (else (loop (cdr xs) (cons (car xs) a)))))) ; ʉ 2 (define (union xs ys) (fold (lambda (x a) (if (memv x ys) a (cons x a))) ys xs))
ŏ̐߂͋W (Xg) ƏW ys ̘a ys ł邱Ƃ¥Ă܂B̐߂ŁAvf (car xs) W ys Ɋ܂܂ĂAVWɉ܂Belse ߂ (car xs) ys Ɋ܂܂ĂȂAWɒlj܂Bʉ named let fold go[WłB
19
Xg : W̐ (define (intersection xs ys) (cond ((null? xs) '()) ((memv (car xs) ys) (cons (car xs) (intersection (cdr xs) ys))) (else (intersection (cdr xs) ys)))) ; ʉ 1 (define (intersection xs ys) (let loop ((xs xs) (a '())) (cond ((null? xs) a) ((memv (car xs) ys) (loop (cdr xs) (cons (car xs) a))) (else (loop (cdr xs) a))))) ; ʉ 2 (define (intersection xs ys) (fold (lambda (x a) (if (memv x ys) (cons x a) a)) '() xs))
ŏ̐߂͋W (Xg) ƏW ys ̐ς͋Wł邱Ƃ¥Ă܂B̐߂ŁAvf (car xs) W ys Ɋ܂܂ĂAVWɒlj܂BłȂAelse ߂ŗvf (car xs) Wɒlj܂Bʉ named let fold go[WłB
20
Xg : W̍ (define (difference xs ys) (cond ((null? xs) '()) ((memv (car xs) ys) (difference (cdr xs) ys)) (else (cons (car xs) (difference (cdr xs) ys))))) ; ʉ 1 (define (difference xs ys) (let loop ((xs xs) (a '())) (cond ((null? xs) a) ((memv (car xs) ys) (loop (cdr xs) a)) (else (loop (cdr xs) (cons (car xs) a)))))) ; ʉ 2 (define (difference xs ys) (fold (lambda (x a) (if (memv x ys) a (cons x a))) '() xs))
ŏ̐߂́AWƏW ys ̍͋Wł邱Ƃ¥Ă܂B̐߂ŁAvf (car xs) ys Ɋ܂܂ꂢꍇ͏Wɂlj܂BłȂAelse ߂ŗvf (car xs) Wɒlj܂Bʉ named let fold go[WłB
21
Xg : Xg̃}[W (define (merge-list pred xs ys) (cond ((null? xs) ys) ((null? ys) xs) ((pred (car xs) (car ys)) (cons (car xs) (merge-list pred (cdr xs) ys))) (else (cons (car ys) (merge-list pred xs (cdr ys))))))
ŏ̐߂́AXgƃXg ys }[W ys ɂȂ邱Ƃ¥Ă܂B̐߂́AXg xs ƋXg}[W xs ɂȂ邱Ƃ¥Ă܂B 2 ̐߂AċAĂяo̒‾ɂȂ܂B
3 Ԗڂ̐߂ŁAꂼ̃Xg̐擪vfq pred ŔrApred ^Ԃꍇ (car xs) }[WXg̐擪ɒljAłȂŌ̐߂ (car ys) }[WXg̐擪ɒlj܂Bmerge-list ċAĂяoƂ́Axs ܂ ys ̐擪vf菜ČĂяoƂɒӂĂB
22
}[W¥[g̓Xg̒ 1, 2, 4, 8, ... Ƒ₵ĂAċAIɍlȒPłB܂A¥[g郊Xg 2 ɕāAO¥[g܂BɁA㔼¥[gāǍʂ}[W킯łB
ċAĂяo邽тɃXg 2 ɕ̂ŁAŌɃXg̗vf͂ЂƂƂȂ܂B̓¥[gς݂̃XgȂ̂ŁAōċAĂяoIă}[Ws킯łBvÔ͎悤ɂȂ܂B
Xg : }[W¥[g (define (merge-sort pred n ls) (if (= n 1) (list (car ls)) (let ((m (quotient n 2))) (merge-list pred (merge-sort pred m ls) (merge-sort pred (- n m) (drop ls m))))))
merge-sort ̈ pred vfrqA ls ¥[g郊XgA n Xg̒¥܂Bmerge-sort ̓Xg鏈ŁAVXgȂƂɒӂĂBmerge-sort ̓¥[g郊Xg͈̔͂Jnʒuƒŕ¥Ă܂BXgꍇAO ls m (= n / 2) ŕ¥A㔼 (drop ls m) (- n m) ŕ¥܂B
Ƃ merge-sort ċAĂяoŃXgĂAXg̒ 1 ɂȂȂΐVXgԂ܂BāAmerge-sort Ń¥[gXg merge-list Ń}[W킯łB
23
Xg : ړ̔ (define (prefix ls ps) (cond ((null? ps) #t) ((eqv? (car ls) (car ps)) (prefix (cdr ls) (cdr ps))) (else #f)))
ړ̔͊ȒPłBŏ̐߂́AXg͐ړł邱Ƃ¥Ă܂B̐߂ŁAXg̐擪vfꍇ́Ac̃Xg (cdr ps) (cdr ls) ̐ړł邱Ƃm߂܂B
24
Xg : ڔ̔ (define (suffix ls ss) (prefix (drop ls (- (length ls) (length ss))) ss))
ڔ̔ȒPłBXg ls ss ̒̍߁Als ̐擪獷̌vf菜܂B ls ss ̒Ȃ̂ŁAƂ prefix Ŕr邾łB
25
Xg : Xg̔ (define (sublist ks ls) (cond ((null? ls) #f) ((prefix ls ks) #t) (else (sublist ks (cdr ls)))))
sublist prefix gƊȒPłBŏ̐߂ ls Xg̏ꍇAks ͕Xgł͂Ȃ̂ #f Ԃ܂B̐߂ŁAks ls ̐ړłΕXgȂ̂ #t Ԃ܂BȊȌꍇ ls ̐擪vf菜āAsublist ċAĂяo邾łB
0 件のコメント:
コメントを投稿