1.4 Prolog dasturida dasturlarning ishlashi
Agar dasturda xatosi bo’lsa bizga Prolog dasturi xatolarini qaysi qatorda ekanligini ko’rsatadi. Masalan birorta dasturdan xatosini topishi ko’ramiz. Bu xatolar 3-rasmda to’liq ko’rsatilgan.
(1.3-rasm) Prolog dasturida xatoliklarning ko’rsatilishi
1.5 Ikki sonning yig’indisini hisoblovchi dastur
Quyidagi dasturda 2ta sonning yig’indisi(sum) yoki 2ta sonning ko’paytmasi(prog)ni hisoblash dasturi keltirilgan. Bu yerda ko’paytma keltirilgan:
implement main
open core domains pp2 = (real Argument1, real Argument2)
-> real ReturnType.
clauses classInfo("main", "hi_order").
class predicates
sum : pp2. prod : pp2.
red : (pp2, real*, real, real) procedure (i, i, i, o). clauses sum(X, Y)= Z :- Z=X+Y. prod(X, Y)= Z :- Z=X*Y.
red(_P, [], A, A) :- !. red(P, [X|Xs], A, Ans) :- red(P, Xs, P(X, A), Ans).
run():- console::init(), red(prod, [1,2,3,4,5], 1, X), stdio::write(X), stdio::nl, succeed().
end implement main goal mainExe::run(main::run).
Bu dasturdan natija 4- rasmda keltirilgan.
(1.4-rasm) Ko’paytmani hisoblovchi dasturni ko’rinishi va natijasi.
2ta argument berilgan. Bu argumentlarning yig’indisini hisoblovchi dastur yuqoridagi dasturga o’xshab ketadi. Faqat bu dasturda yig’indi hisoblanadi.
implement main open core domains pp2 = (real Argument1, real Argument2) -> real ReturnType. clauses
% classInfo("main", "hi_order").
class predicates sum : pp2. prod : pp2.
red : (pp2, real*, real, real) procedure (i, i, i, o). clauses sum(X, Y)= Z :- Z=X+Y. prod(X, Y)= Z :- Z=X*Y.
red(_P, [], A, A) :- !. red(P, [X|Xs], A, Ans) :- red(P, Xs, P(X, A), Ans).
run():- console::init(), red(sum, [1,2,3,4,5], 1, X), stdio::write(X), stdio::nl, succeed().
end implement main goal mainExe::run(main::run).
Dasturdan natija yig’indi sifatida chiqadi.
1dan 10 gacha bo’lgan sonlar yig’indisini chiqaruvchi dastur tuzamiz. Bu dastur quyidagicha:
implement main open core domains pp2 = (real Argument1, real Argument2) -> real ReturnType. clauses
% classInfo("main", "hi_order").
class predicates sum : pp2. prod : pp2. red : (pp2, real*, real, real) procedure (i, i, i, o). clauses sum(X, Y)= Z :- Z=X+Y. prod(X, Y)= Z :- Z=X+Y. red(_P, [], A, A) :- !. red(P, [X|Xs], A, Ans) :- red(P, Xs, P(X, A), Ans).
run():- console::init(), red(prod, [1,2,3,4,5,6,7,8,9],10,X), stdio::write(X), stdio::nl, succeed().
end implement main goal mainExe::run(main::run).
Bu dasturni ishlatsak bizni ekranimizga quyidagicha rasm chiqadi. Bu dasturni ishlatish uchun ham Menyular satridan Build menyusini topamiz va undan Run In Window tugmasini bosamiz. Bizga Prolog dasturlash tili har doimgiday Cancel tugmasi chiqaradi va uni bosamiz. Bu oynada natija paydo bo’ladi va yechim deyiladi. Bunday natija 5-rasmda ko’rsatilgan.
(1.5-rasm) 1dan 10 gacha bo’lgan sonlar yig’indisi dasturidan olingan natijaning umumiy ko’rinishi
Do'stlaringiz bilan baham: |