0% found this document useful (0 votes)
8 views

Lab Report factorial tail recursion

The document contains multiple lab reports on programming in Prolog, focusing on calculating factorials and Fibonacci numbers using tail recursion, implementing logic gates, and creating family trees. Each section outlines objectives, code implementations, explanations, and conclusions regarding the effectiveness and correctness of the implementations. The reports emphasize the clarity and organization of the code, demonstrating Prolog's capabilities in handling recursion and logical relationships.

Uploaded by

urushajoshi521
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab Report factorial tail recursion

The document contains multiple lab reports on programming in Prolog, focusing on calculating factorials and Fibonacci numbers using tail recursion, implementing logic gates, and creating family trees. Each section outlines objectives, code implementations, explanations, and conclusions regarding the effectiveness and correctness of the implementations. The reports emphasize the clarity and organization of the code, demonstrating Prolog's capabilities in handling recursion and logical relationships.

Uploaded by

urushajoshi521
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lab Report: Factorial using tail Recursion

Objectives:

 Develop a program to calculate factorials


 Manage both the base and recursive cases effectively
 Verify the correctness of the function
 Ensure the code is well-structured and easy to follow

Introduction:

In this lab, we examine how to compute the factorial of a number using Prolog. The factorial
operation involves multiplying a number by all positive integers less than itself. For instance, the
factorial of 5 (written as 5!) is 5 * 4 * 3 * 2 * 1, which equals 120. We will create a Prolog predicate
to calculate the factorial using recursion. The implementation includes a base case for the factorial
of 0 and a recursive case for positive integers.

Code Implementation:

Factorial (version 1)

%fact

factorial(0,1).

%rule

factorial(N,F):-

N>0,

N1 is N-1,

factorial(N1,F1),

F is F1 * N.

Factorial (version 2: tail Recursion)


fact(N, F) :-

fact(N, 1, F).

fact(0, Acc, Acc).

fact(N, Acc, F) :-

N > 0,

NewAcc is N * Acc,

NewN is N - 1,

fact(NewN, NewAcc, F).

Output:

Code Explanation:

The given Prolog code calculates the factorial of a number using these predicates:

fact(N, F):

This is the main predicate that initiates the factorial calculation.

It calls fact/3 with an initial accumulator value of 1.

fact(0, Acc, Acc):

This is the base case of the recursion.

When N is 0, the accumulator (Acc) holds the final result, which is the factorial of the original
number.

fact(N, Acc, F):


This is the recursive case.

If N is greater than 0, it calculates the new accumulator value (NewAcc) by multiplying the current
Acc with N.

It then decrements N by 1 and recursively calls itself until N reaches 0.

Conclusion:

This lab demonstrated how to compute the factorial of a number using Prolog. We established a
base case for when the number is 0 and a recursive case for positive numbers. Testing the
implementation with various values confirmed its correctness. Prolog’s support for recursion
makes it well-suited for this task, and the organized code structure ensures the calculation is both
straightforward and efficient.
Lab Report: Fibonacci using tail Recursion

Objectives:

 Develop a program to calculate Fibonacci numbers


 Manage both the base and recursive cases effectively
 Validate the function through testing
 Ensure the code is organized and easy to understand

Introduction:

In this lab, we focus on computing Fibonacci numbers using Prolog. The Fibonacci sequence is a
series in which each number is the sum of the two previous numbers, starting from 0 and 1. For
instance, the sequence begins as 0, 1, 1, 2, 3, 5, 8, and so forth.

We develop a Prolog predicate to calculate Fibonacci numbers, employing recursion to determine


the value at a specific position in the sequence. The implementation includes a base case for when
N equals 0 and a recursive case to handle positive numbers.

Code Implementation:

fibo(N,F):-

fibo_helper(N,0,1,F).

fibo_helper(0,Acc, _, Acc).

fibo_helper(N, Prev, Curr, Res):-

N>0,

Next is Prev + Curr,

N1 is N-1,

fibo_helper(N1,Curr,Next,Res).
Output:

Code Explanation:

The Prolog code calculates Fibonacci numbers using these predicates:

 fibo(N, F): Initiates the Fibonacci calculation by calling fibo_helper/4 with starting values.

 fibo_helper(0, Acc, _, Acc): Base case; when N is 0, the accumulator (Acc) holds the
result.

 fibo_helper(N, Prev, Curr, Res): Recursive case; calculates the next Fibonacci number
by adding Prev and Curr, then recurses with updated values until N is 0.

Conclusion:

This lab demonstrated how to compute Fibonacci numbers using Prolog. The implementation
includes a base case for when N is 0 and a recursive case for positive values. Testing the code with
different inputs confirmed its correctness. Prolog's use of recursion effectively manages Fibonacci
calculations, and the clear code structure enhances readability and understanding.
Lab Report: Logic Gates Implementation

Objectives:

 Develop and implement various logic gates.


 Establish rules that define the behavior of each gate.
 Test the functionality of the gates using different input combinations.
 Maintain simplicity and clarity in the code design.

Introduction:

In this lab, we examine how to model basic logic gates using Prolog. Logic gates are crucial elements in
digital circuits and computing systems. We concentrate on implementing three primary types of gates:

 AND Gate: Produces true only when both inputs are true.
 OR Gate: Produces true if at least one of the inputs is true.
 NOT Gate: Outputs the inverse of the input.
 XOR Gate: Outputs true if exactly one of the inputs is true.
We will create Prolog predicates to represent these gates' behavior and test them with different inputs to
verify their accuracy.

Code Implementation:

AND Gate:

%AND gate using McCulloch-Oitts neuron

and_gate(X1,X2,Output):-

(X1 == 0; X1 == 1), % EnSure X1 is either 0 or 1

(X2 == 0; X2 == 1), % Ensure X2 is either 0 or 1

W1 is 1,

W2 is 1,
Theta is 2,

Sum is X1 * W1 + X2 * W2,

(Sum >= Theta -> Output is 1; Output is 0).

Output:

OR Gate:

% OR_gate

or_gate(X1, X2, Output) :-

W1 is 1,

W2 is 1,

Theta is 1,

Sum is X1 * W1 + X2 * W2,

(Sum >= Theta -> Output is 1 ; Output is 0).

Output:
NOT Gate:

% NOT_gate

not_gate(X, Output) :-

W is -1,

Theta is 0,

Sum is X * W,

(Sum >= Theta -> Output is 1 ; Output is 0).

Output:

XOR Gate:

% XOR gate
xor_gate(X1, X2, Output) :-

or_gate(X1, X2, OrOutput),

and_gate(X1, X2, AndOutput),

not_gate(AndOutput, NotAndOutput),

and_gate(OrOutput, NotAndOutput, Output).

Output:

Conclusion:

This lab showcased the implementation of fundamental logic gates using Prolog. We modeled the AND,
OR, NOT, and XOR gates by defining predicates and tested them with different input values. The
implementation correctly mirrors the behavior of these gates, and the organized code structure makes the
logic straightforward and easy to comprehend.
Lab Report: Creating Family tree

Objectives:

 Create Prolog rules to represent family relationships.


 Account for various parent-child combinations.
 Test the functionality of the Prolog rules.
 Ensure the rules operate efficiently with the provided facts.
 Maintain clarity and simplicity in the implementation.
 Evaluate the logic and performance of the rules.

Introduction:

Prolog is a programming language specialized for knowledge representation and logical reasoning. In this
lab, we use Prolog to model family relationships. By defining straightforward facts and rules, we can
determine relationships such as son, daughter, mother, father, or grandfather.

We start by establishing basic relationships using facts. Next, we define rules to derive more complex
relationships based on these initial facts. The facts provide foundational information, while the rules
enable us to uncover additional connections.

Implementation:

%fact

parent(john,bob).

parent(john,samantha).

parent(mary,bob).

parent(mary,samantha).

parent(donald,john).
male(john).

male(bob).

male(donald).

female(mary).

female(samantha).

%rule

son(X,Y):-parent(Y,X),male(X).

daugther(X,Y):- parent(Y,X),female(X).

mother(X,Y):-parent(X,Y),female(X).

father(X,Y):- parent(X,Y), male(X).

grandfather(X,Y):- parent(X,Z), parent(Z,Y), male(X).

Output:
Conclusion:

This lab demonstrated how to model family relationships in Prolog using facts and rules. By establishing
foundational information, such as parental relationships and gender, and creating rules to determine
more complex connections like son, daughter, mother, father, or grandfather, we can effectively analyze
and understand family structures. Prolog’s logical reasoning capabilities make it a powerful tool for this
kind of symbolic representation.

You might also like