Thursday, 14 February 2019

Prolog: Structures


A structure is an object that holds collection of other objects called components.

For example, an employee object is a structure, it holds, employee name, experience, age, current project details etc.,

Each object in the structure can be broken down further lik project details contain, project name, number of persons working in the project, budget allocated, project current version etc.,

Why structures?
Structures are used to combine the related information in one object.

Syntax of structure
A structure is defined using a functor followed by the arguments.

Syntax
functor(argument1, argument2 …argumentN)

Example
employee(1, personalDetails(krishna, gurram), projectDetails(cpcs, 10)).

In the above example,
         employee is the functor followed by three arguments,

structures.pl
employee(1, personalDetails(krishna, gurram), projectDetails(cpcs, 10)).
employee(2, personalDetails(gopi, battu), projectDetails(dataExtractor, 23)).

How to query all the details of employee with id 1?

employee(1, personalDetails(FirstName, LastName), projectDetails(ProjectName, NoOfPeopleWorking)).

?- employee(1, personalDetails(FirstName, LastName), projectDetails(ProjectName, NoOfPeopleWorking)).
FirstName = krishna,
LastName = gurram,
ProjectName = cpcs,
NoOfPeopleWorking = 10.


How to query all the employee details?
?- employee(_, personalDetails(FirstName, LastName), projectDetails(ProjectName, NoOfPeopleWorking)).
FirstName = krishna,
LastName = gurram,
ProjectName = cpcs,
NoOfPeopleWorking = 10 ;
FirstName = gopi,
LastName = battu,
ProjectName = dataExtractor,
NoOfPeopleWorking = 23.


Use ; to get next result.

1 ?- consult(structures).
true.

2 ?- employee(1, personalDetails(FirstName, LastName), projectDetails(ProjectName, NoOfPeopleWorking)).
FirstName = krishna,
LastName = gurram,
ProjectName = cpcs,
NoOfPeopleWorking = 10.

3 ?- employee(2, personalDetails(FirstName, LastName), projectDetails(ProjectName, NoOfPeopleWorking)).
FirstName = gopi,
LastName = battu,
ProjectName = dataExtractor,
NoOfPeopleWorking = 23.

4 ?- employee(_, personalDetails(FirstName, LastName), projectDetails(ProjectName, NoOfPeopleWorking)).
FirstName = krishna,
LastName = gurram,
ProjectName = cpcs,
NoOfPeopleWorking = 10 ;
FirstName = gopi,
LastName = battu,
ProjectName = dataExtractor,
NoOfPeopleWorking = 23.



Previous                                                 Next                                                 Home

No comments:

Post a Comment