0% found this document useful (0 votes)
8 views4 pages

Prolog Functions for Population and Lists

Uploaded by

amereslam730
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 views4 pages

Prolog Functions for Population and Lists

Uploaded by

amereslam730
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

Name: Assem Khaled Abdelaziz Elnahas

Code: 42022615

. Prolog section Task.


Q1.
population(egypt, 104000000).
population(usa, 331000000).

area(egypt, 1001450).
area(usa, 9834000).

density(Country, Density) :-
population(Country, Pop),
area(Country, Area),
Area > 0,
Density is Pop / Area.
Q2. % Base case: Head is the element we're looking for.
member(X, [X|_]).
Name: Assem Khaled Abdelaziz Elnahas
Code: 42022615

% Recursive case: Check in the Tail.


member(X, [_|T]) :-
member(X, T).

Q3. area_of_rectangle(Width, Height, Area) :-


Width > 0,
Height > 0,
Area is Width * Height.
Q4. % Base case: factorial of 0 is 1
factorial(0, 1).

% Recursive case: N > 0


factorial(N, F) :-
N > 0,
N1 is N - 1,
Name: Assem Khaled Abdelaziz Elnahas
Code: 42022615

factorial(N1, F1),
F is N * F1.
Q5. % reply(InputSentence, ReplySentence)

% Rule 1: Match a specific sentence


reply([what, are, the, boundary, conditions],
[the, boundary, conditions, depend, on, the, problem]).

% Rule 2: Match a specific sentence structure


reply([you, are, information, system, student],
[no, i, am, computer, science, student]).
Q6. % Base case: empty list has length 0
listlen([], 0).

% Recursive case: list has head and tail


listlen([_|T], N) :-
Name: Assem Khaled Abdelaziz Elnahas
Code: 42022615

listlen(T, N1),
N is N1 + 1.
Q7. % Wrapper to start with 0 accumulator
listlen(L, N) :-
listlen_acc(L, 0, N).

% Base case: empty list, return accumulator as result


listlen_acc([], Acc, Acc).

% Recursive case: increase accumulator by 1


listlen_acc([_|T], Acc, N) :-
Acc1 is Acc + 1,
listlen_acc(T, Acc1, N).

You might also like