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

what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
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