2010年9月7日火曜日

Prologの技芸 4.2節の練習問題

(1)
挿入ソート
sort([3,1,2],Ys)
sort([1,2],Zs1)
sort([2],Zs2)
sort([],Zs3) {Zs3=[]}
true
insert(2,[],Zs2) {Zs2=[2]}
true
insert(1,[2],Zs1) {Zs1=[2|Zs4]}
1>2
fail
insert(1,[2],Zs1) {Zs1=[1,2|[]]}
1=<2
true
insert(3,[1,2],Ys) {Ys=[1|Ys1]}
3>1
true
insert(3,[2],Ys1) {Ys1=[2|Ys2]}
3>2
true
insert(3,[],Ys2) {Ys2=[3]}
true


クイックソート
sort([3,1,2],Ys)
partition([1,2],3,Ls,Bs) {Ls=[1|Ls1]}
1 =< 3
true
partition([2],3,Ls1,Bs) {Ls1=[2|Ls2]}
2 =< 3
true
partition([],3,Ls2,Bs) {Ls2=[], Bs=[]}
true
sort([1,2],Ls3)
partition([2],1,Ls4,Bs1) {Ls4=[2|Ls5]}
2 =< 1
fail
partition([2],1,Ls4,Bs1) {Bs1=[2|Bs2]}
2 > 1
true
partition([],1,Ls4,Bs2) {Ls4=[], Bs2=[]}
true
sort([],Ls5) {Ls5=[]}
true
sort([2],Bs3)
partition([],2,Ls6,Bs4) {Ls6=[], Bs4=[]}
true
sort([],Ls7) {Ls7=[]}
true
sort([],Bs5) {Bs5=[]}
true
append([],[2],Bs3) {Bs3=[2]}
true
append([],[1,2],Ls3) {Ls3=[1,2]}
true
sort([],Bs6) {Bs6=[]}
true
append([1,2],[3],Ys) {Ys=[1,2,3]}
true


(2)
derivative(3*sin(x)-4*cos(x),x,D) {D=0}
constant(3*sin(x)-4*cos(x))
integer(3*sin(x)-4*cos(x))
fail
derivative(3*sin(x)-4*cos(x),x,D) {D=DF-DG}
derivative(3*sin(x),x,DF) {DF=0}
constant(3*sin(x))
integer(3*sin(x))
fail
derivative(3*sin(x),x,DF) {DF=3*DG1+DF1*sin(x)}
derivative(3,x,DF1) {DF1=0}
constant(3)
integer(3)
true
derivative(sin(x),x,DG1) {DG1=0}
constant(sin(x))
integer(sin(x))
fail
derivative(sin(x),x,DG1) {DG1=cos(x)}
true
derivative(4*cos(x),x,DG) {DG=0}
constant(4*cos(x))
integer(4*cos(x))
fail
derivative(4*cos(x),x,DG) {DG=4*DG1+DF1*cos(x)}
derivative(4,x,DF1) {DF1=0}
constant(4)
integer(4)
true
derivative(cos(x),x,DG1) {DG1=0}
constant(cos(x))
integer(cos(x))
fail
derivative(cos(x),x,DG1) {DG1=-sin(x)}
true

0 件のコメント: