<< 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

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
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