Recursion Basic ,List
Manipulation and Function
Prof. Neeraj Bhargava
Kapil Chauhan
Department of Computer Science
School of Engineering & Systems Sciences
MDS University, Ajmer
contents
 Recursion Introduction
 List
 List manipulation
 List manipulation Example
 Functions
 Functions example
Recursion Introduction
 The recursion in any language is a function that can call
itself until the goal has been succeed.
 The best way in Prolog to calculate a factorial is to do it
recursivelly. Here is an example of how it can be done :
 factoriel(0,1).
 factoriel(X,Y)
 :- X1 is X - 1,
 factoriel(X1,Z),
 Y is Z*X,!.
after the first solution. Now if you enter :
?- factoriel(5,X).
X = 120
Yes
list
 we have only considered simple items as arguments
to our programs.
 [first,second,third] = [A|B]
 where A = first and B=[second,third]
 List is one of the most important data structure in
Prolog.
 Here are some example simple lists
[a,b,c,d,e,f,g]
[apple,pear,bananas,breadfruit]
 Consider the following fact.
?- p([a,b,c], X, Y).
 X=a
 Y=[b,c]
 yes
list manipulation
 The first element of a list is called its head and the
remaining list is called the tail.
 An empty list doesn’t have a head.
 A list just containing a single element has a head
 [Nepal | [India, Bhutan, Sri Lanka, Maldives,
Pakistan, Bangladesh]]
 Nepal is head and remaining countries constitute tail
and this is represented as
output
?- sky([1,2,3,4,5,6],X).
X = [1, 3, 5, 2, 6, 4] ;
false.
?- sky([1,2,3,4,5,6,7,8,9],X).
X = [1, 3, 5, 7, 9, 2, 6, 4, 8] ;
false.
functions
 Defining four functions (it’s called predicates in prolog),
addition, subtraction, multiplication, and division.
 Each of them takes three parameters, the result is saved to
the third parameter.
 Functions are normal Prolog predicates.
 Some predicates are already predefined when you start your
Prolog system. These are called built-in predicates or
simply built-ins.
Functions example
addition(A, B, C):- C is A + B.
subtraction(A, B, C):- C is A - B.
multiplication(A, B, C):- C is A * B.
division(A, B, C):- C is A / B.
?- addition(2,3,X).
X = 5.
?- subtraction(4,2,X).
X = 2.
?- multiplication(3,4,K).
K = 12.
?- division(9, 3, D).
D = 3.
Assignment
 Explain List and function manipulation in prolog with
example

List manipulation

  • 1.
    Recursion Basic ,List Manipulationand Function Prof. Neeraj Bhargava Kapil Chauhan Department of Computer Science School of Engineering & Systems Sciences MDS University, Ajmer
  • 2.
    contents  Recursion Introduction List  List manipulation  List manipulation Example  Functions  Functions example
  • 3.
    Recursion Introduction  Therecursion in any language is a function that can call itself until the goal has been succeed.  The best way in Prolog to calculate a factorial is to do it recursivelly. Here is an example of how it can be done :  factoriel(0,1).  factoriel(X,Y)  :- X1 is X - 1,  factoriel(X1,Z),  Y is Z*X,!. after the first solution. Now if you enter : ?- factoriel(5,X). X = 120 Yes
  • 4.
    list  we haveonly considered simple items as arguments to our programs.  [first,second,third] = [A|B]  where A = first and B=[second,third]  List is one of the most important data structure in Prolog.  Here are some example simple lists [a,b,c,d,e,f,g] [apple,pear,bananas,breadfruit]  Consider the following fact. ?- p([a,b,c], X, Y).  X=a  Y=[b,c]  yes
  • 5.
    list manipulation  Thefirst element of a list is called its head and the remaining list is called the tail.  An empty list doesn’t have a head.  A list just containing a single element has a head  [Nepal | [India, Bhutan, Sri Lanka, Maldives, Pakistan, Bangladesh]]  Nepal is head and remaining countries constitute tail and this is represented as
  • 6.
    output ?- sky([1,2,3,4,5,6],X). X =[1, 3, 5, 2, 6, 4] ; false. ?- sky([1,2,3,4,5,6,7,8,9],X). X = [1, 3, 5, 7, 9, 2, 6, 4, 8] ; false.
  • 7.
    functions  Defining fourfunctions (it’s called predicates in prolog), addition, subtraction, multiplication, and division.  Each of them takes three parameters, the result is saved to the third parameter.  Functions are normal Prolog predicates.  Some predicates are already predefined when you start your Prolog system. These are called built-in predicates or simply built-ins.
  • 8.
    Functions example addition(A, B,C):- C is A + B. subtraction(A, B, C):- C is A - B. multiplication(A, B, C):- C is A * B. division(A, B, C):- C is A / B. ?- addition(2,3,X). X = 5. ?- subtraction(4,2,X). X = 2. ?- multiplication(3,4,K). K = 12. ?- division(9, 3, D). D = 3.
  • 9.
    Assignment  Explain Listand function manipulation in prolog with example