A repository for Artificial Intelligance (AI) for B.Sc. (H) C.S.
[dbname].
consult('dbname.pl').
Steps:-
trace.
notrace.
or nodebug.
to go normally
- Pradicates:
A fact/prodicate is a type of the clause that defines a over 2 given atoms. Simply a fact is a relationship of atom1 with atom2
Syntax:pradicate(atom1, atom2).
- Atom:
An atom starts with lower-case letter
Can contain - Alphabets, Numbers, + - _ * / < >
White space are effective if put into quotes eg.'romeo juliet'
- Rule:
Comes into play when a fact is depends upon some other facts
A rule is used to defile a condition.
Syntax:<condition> :- <action>
- Variables:
A variable stores a value like atoms
starts with upper-case letter
Eg.afact(first, second). afact(first, X).
it returns ----> X = second
- Operators:
= Equals : alice = alice (TRUE)
+ Not-Equals : + (3 = 4) (TRUE)
> Greater : 3 > 15 (FALSE)
<, =<, >=
one(X, q) = one(r, Y) : Gives -> X = r, Y = q
=:= Equality of Expression : 5+4 =:= 4+5. (TRUE)
== Non-Equality of Expression : 5+4 =:= 4+5. (False)
; OR : 5 > 100 ; 10 < 100>. (TRUE)
, AND : 5 > 100 , 10 < 100>. (FALSE)- Others:
is Assignment : Var is 10 (assigns 10 to Var)
+, -,
/ Devision : gives decimal result
// Devision : gives result without decimal
*, **- Functions:
mod(A, B) Remainder : mod(7, 5) (gives 2)
random(A, B, X) : generates a random b/w A and B (including A and B), and stores in X
between(A, B, X) : generates all possible from A to B, and stores in X
succ(A, X) Increment by one: similar to X = A++; in C++
abs() Absolute val. : X is abs(-4) (assigns 4 to X)
max(A, B) : max val b/w A and B
min(A, B) : min val b/w A and B
round(Num) For floats : round(12.763) gives 13
truncate(Num) Remove Decimal : truncate(12.98) gives 12
floor(), ceiling(), sqrt(), sin() …., asin() …., log(), log10(), exp, pi, e
write() ---> outputs writeq() ---> outputs in "" format() ---> formatted output read(Var) ---> reads (any) input into Var get(Var) ---> SINGLE CHAR input & stores ASCII val into Var
Writing :
open(File, write, Stream), write(Stream, Text), close(Stream).
Reading :
open(File, read, Stream), get_char(Stream, Char), /* function below ain't inbuilt */ process_stream(Char, Stream), close(Stream). /* defining process_stream(Char, Stream) */ /* if file has ended */ process_stream(end_of_file, _) :- !. /* what to do after reading char?? */ process_stream(Char, Stream) :- write(Char), get_char(Stream, Char), process_stream(Char, Stream).
File : file name in quotes
write/read : mode of opening
Stream : kinda buffer or connection esteblisher with flie
get_char() : takes one char from file
! : stops back-tracking
:- dynamic(predicate/Num). : :- dynamic(). : makes a database’s predicate changable while using same [database]
predicate : name of predicate to make it dynamic
Num : number of occurances of that pradicate in database
After that, following can be used to change/add pradicates:
assertz() adds at End : eg.
assertz(happy(person))
asserta() adds at Beg. : eg.asserta(happy(person))
retract() removes : eg.retract(happy(person))
retractall() removes All those predicates : eg.retractall(parent(_,_))
Lists can have any no. of data in them.
member(element, List) : TRUE, if element belongs to the List
also –>member(Ele, List)
: Ele cycles through all elements in List
reverse(List, X) : Reverses List and stores in X
append(List1, List2, X) : X = [, ]
**atom_length(Str, Len)**
**name()** : String <--> List
**join_str(A, B, ...)** : joins any no. of str.