<< Chapter < Page Chapter >> Page >

Ví dụ: “Turbo prolog 2.0”

  1. Symbol: Là một kiểu sơ cấp, có hình thức giống chuỗi ký tự. Hằng symbol có hai dạng: Dãy các chữ, số và dấu gạch dưới viết liên tiếp, ký tự đầu phải viết thường (chẳng hạn: telephone_number); Dãy các ký tự ở giữa một cặp hai nháy kép (giống như chỗi ký tự)
  2. Một số phép toán của các kiểu

Phép toán số học

Phép toán Ý nghĩa Kiểu của đối số Kiểu kết quả
+ Cộng hai số Integer, real giống kiểu đối số
- Trừ hai số Integer, real giống kiểu đối số
* Nhân hai số Integer, real giống kiểu đối số
/ Chia hai số Integer, real giống kiểu đối số
Mod Phép chia lấy phần dư Integer Integer
Div Phép chia lấy phần nguyên Integer Integer

Phép toán quan hệ

Phép toán Ý nghĩa Kiểu của đối số Kết quả
< Nhỏ hơn Char, integer, real, string Yes hoặc No
<= Nhỏ hơn hay bằng Char, integer, real, string Yes hoặc No
= Bằng Char, integer, real, string Yes hoặc No
> Lớn hơn Char, integer, real, string Yes hoặc No
>= Lớn hơn hay bằng Char, integer, real, string Yes hoặc No
<>hay>< Khác Char, integer, real, string Yes hoặc No

Các vị từ như các hàm toán học

Vị từ Ý nghĩa Kiểu của đối số Kiểu kết quả Ví dụ
Sin(X) Tính sin của X real real
Tan(X) Tính tang của X real real
Arctan(X) Tính arctang của X real real
Exp(X) Tính eX real real
Ln(X) Tính logarit cơ số e của X real real
Log(X) Tính Logarit cơ số 10 của X real real
SQRT(X) Tính căn bậc hai của X real real
ROUND(X) Cho ta số nguyên là số X được làm tròn, dấu là dấu của X real integer round(2.3)=2round(2.5)=3round(-2.5)=-2round(-2.6)=-3
TRUNC(X) Cho phần nguyên của số X, dấu là dấu của X real integer trunc(2.5)=2trunc(-2.6)=-2
ABS(X) Cho ta trị tuyệt đối của X real real
Random(X) Cho ta số thực X nằm trong khoảng [0, 1) real real
Random(Y, X) Cho ta số nguyên X nằm trong khoảng [0, Y) real integer

Toán tử NOT(X) : Nếu X là Yes thì NOT(X) là No và ngược lại.

Các kiểu dữ liệu do người lập trình định nghĩa

  1. Kiểu mẩu tin:

Cú pháp:<tên kiểu mẩu tin>= tên mẩu tin (danh sách các kiểu phần tử)

Ví dụ:

Domains

ten, tac_gia, nha_xb, dia_chi = string

nam, thang, so_luong = integer

dien_tich = real

nam_xb = nxb(thang, nam)

do_vat = sach(tac_gia, ten, nha_xb, nam_xb); xe(ten, so_luong); nha(dia_chi, dien_tich)

predicates

so_huu(ten,do_vat)

clauses

so_huu(“Nguyen Van A”, sach(“Do Xuan Loi”, “Cau truc DL”, “Khoa hoc Ky thuat”, nxb(8,1985))).

so_huu(“Le thi B”, xe(“Dream II”, 2)).

so_huu(“Nguyen Huu C”, nha(“3/1 Ly Tu Trong, tp Can Tho”, 100.5))

  1. Kiểu danh sách

Cú pháp:<tên kiểu danh sách>=<tên kiểu phần tử>*

Ví dụ:

Domains

intlist = integer*

Một danh sách là một dãy các phần tử phân cách nhau bởi dấu phẩy và đặt trong cặp dấu ngoặc vuông.

Ví dụ:

[]% Danh sách rỗng

[1,2,3] % Danh sách gồm ba số nguyên 1, 2 và 3.

Cấu trúc của danh sách bao gồm hai phần: Phần đầu là phần tử đầu tiên của danh sách và phần đuôi là một danh sách của các phần tử còn lại.

Danh sách được viết theo dạng [X|Y] thì X là phần tử đầu và Y là danh sách đuôi. Chẳng hạn trong danh sách [1,2,3]thì đầu là số nguyên 1 và đuôi là danh sách [2,3].

Trong danh sách cũng có thể dùng biến tự do, chẳng hạn ta có thể viết [_|Y] để chỉ một danh sách có đầu là một phần tử nào đó và có đuôi là danh sách Y.

Các hàm xuất nhập chuẩn

Xuất ra màn hình

  1. Write( Arg1, Arg2, … ,Argn) in ra màn hình giá trị của các đối số.
  2. Writef( Formatstring, Arg1, Arg2, … ,Argn) in ra màn hình giá trị của các đối số theo định dạng được chỉ định trong Formastring.

Trong đó Formastring là một chuỗi có thể là:

  • “%d”: In số thập phân bình thường; đối số phải là char hoặc integer.
  • “%c”: Đối số là một số integer, in ký tự có mã Ascci là đối số đó, chẳng hạn writef(“%c”,65) được A.
  • “%e”: In số thực dưới dạng lũy thừa của 10.
  • “%x”: In số Hexa; đối số phải là char hoặc integer.
  • “%s”: In một chuỗi hoặc một symbol.

Nhập vào từ bàn phím

  1. Readln(X): Nhập một chuỗi ký tự vào biến X.
  2. ReadInt(X): Nhập một số nguyên vào biến X.
  3. ReadReal(X): Nhập một số thực vào biến X.
  4. ReadChar(X): Nhập vào một ký tự vào biến X.

Kỹ thuật đệ quy

Đệ quy là kỹ thuật lập trình được sử dụng trong nhiều ngôn ngữ. Trong Turbo Prolog ta sử dụng đệ quy khi một vị từ được định nghĩa nhờ vào chính vị từ đó.

Như đã nói trong chương lập trình hàm, trong chương trình đệ quy phải có ít nhất một trường hợp dừng và lời gọi đệ quy phải chứa yếu tố dẫn đến trường hợp dừng. Trong Prolog, trường hợp dừng được thể hiện bằng một sự kiện, yếu tố dẫn đến trường hợp dừng thể hiện bằng một biến, liên hệ với biến ban đầu bởi một công thức.

Ví dụ 1: Tính n giai thừa.

Predicates

Facto (integer, integer)

Clauses

Facto(0,1):- !.

Facto(N, FactN) :- N>0, M = N – 1, facto(M, factM), factN = N*factM.

Ở ví dụ trên ta đã định nghĩa một vị từ dùng để tính giá trị giai thừa của một số tự nhiên, đối số thứ nhất là số cần tính giai thừa và đối số thứ hai dùng để nhận giá trị trả về.

Trường hợp dừng ở đây được xác đinh bởi sự kiện 0 giai thừa là 1.

Để tính N! ta tính M! với M= N-1. Yếu tố dẫn đến trường hợp dừng là biến M có giá trị bằng N-1.

Ví dụ 2: Xác định một phần tử trong danh sách các symbol

domains

symbol_list = symbol*

predicates

element1(integer,symbol_list,symbol)

element (integer,symbol_list,symbol)

clauses

% element1 không suy diễn ngược được

element1(1,[X|_],X).

element1(N,[_|L],Y):-M=N-1,

element1(M,L,Y).

% element có thể suy diễn ngược

element(1,[X|_],X).

element(N,[_|L],Y):-element(M,L,Y),

N=M+1.

Sự suy diễn thuận chiều là cho danh sách và vị trí, tìm được phần tử tại vị trí đó, chẳng hạn, nếu ta đưa vào goal element(2,[a,b,c,d],X) ta được X=b.

Sự suy diễn ngược ở đây là cho danh sách và phần tử, tìm được vị trí của phần tử đó, chẳng hạn, nếu ta đưa vào goal element(N,[a,b,c,d], b) ta được N=2.

Ví dụ 3: Sắp xếp một danh sách các số nguyên

domains

list=integer*

predicates

insert(integer,list,list)

sort(list,list)

clauses

insert(E,[],[E]).

insert(E,[A|B],[E,A|B]):-E<=A.

insert(E,[A|B],[A|C]):-E>A,insert(E,B,C).

sort([],[]).

sort([X|R1],L):-sort(R1,R),

insert(X,R,L).

Questions & Answers

A golfer on a fairway is 70 m away from the green, which sits below the level of the fairway by 20 m. If the golfer hits the ball at an angle of 40° with an initial speed of 20 m/s, how close to the green does she come?
Aislinn Reply
cm
tijani
what is titration
John Reply
what is physics
Siyaka Reply
A mouse of mass 200 g falls 100 m down a vertical mine shaft and lands at the bottom with a speed of 8.0 m/s. During its fall, how much work is done on the mouse by air resistance
Jude Reply
Can you compute that for me. Ty
Jude
what is the dimension formula of energy?
David Reply
what is viscosity?
David
what is inorganic
emma Reply
what is chemistry
Youesf Reply
what is inorganic
emma
Chemistry is a branch of science that deals with the study of matter,it composition,it structure and the changes it undergoes
Adjei
please, I'm a physics student and I need help in physics
Adjanou
chemistry could also be understood like the sexual attraction/repulsion of the male and female elements. the reaction varies depending on the energy differences of each given gender. + masculine -female.
Pedro
A ball is thrown straight up.it passes a 2.0m high window 7.50 m off the ground on it path up and takes 1.30 s to go past the window.what was the ball initial velocity
Krampah Reply
2. A sled plus passenger with total mass 50 kg is pulled 20 m across the snow (0.20) at constant velocity by a force directed 25° above the horizontal. Calculate (a) the work of the applied force, (b) the work of friction, and (c) the total work.
Sahid Reply
you have been hired as an espert witness in a court case involving an automobile accident. the accident involved car A of mass 1500kg which crashed into stationary car B of mass 1100kg. the driver of car A applied his brakes 15 m before he skidded and crashed into car B. after the collision, car A s
Samuel Reply
can someone explain to me, an ignorant high school student, why the trend of the graph doesn't follow the fact that the higher frequency a sound wave is, the more power it is, hence, making me think the phons output would follow this general trend?
Joseph Reply
Nevermind i just realied that the graph is the phons output for a person with normal hearing and not just the phons output of the sound waves power, I should read the entire thing next time
Joseph
Follow up question, does anyone know where I can find a graph that accuretly depicts the actual relative "power" output of sound over its frequency instead of just humans hearing
Joseph
"Generation of electrical energy from sound energy | IEEE Conference Publication | IEEE Xplore" ***ieeexplore.ieee.org/document/7150687?reload=true
Ryan
what's motion
Maurice Reply
what are the types of wave
Maurice
answer
Magreth
progressive wave
Magreth
hello friend how are you
Muhammad Reply
fine, how about you?
Mohammed
hi
Mujahid
A string is 3.00 m long with a mass of 5.00 g. The string is held taut with a tension of 500.00 N applied to the string. A pulse is sent down the string. How long does it take the pulse to travel the 3.00 m of the string?
yasuo Reply
Who can show me the full solution in this problem?
Reofrir Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Ngôn ngữ lập trình. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10783/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Ngôn ngữ lập trình' conversation and receive update notifications?

Ask