-
Notifications
You must be signed in to change notification settings - Fork 1
/
linear_search.pl
27 lines (22 loc) · 1.93 KB
/
linear_search.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
%****************************************************************************************************
% *
% Linear Search in a List *
% *
% The given Prolog code defines predicates to perform a linear search for an element in a list.*
% The `linear_search` predicate is defined with two cases: *
% 1. Base case: The element is found at the head of the list. *
% 2. Recursive case: The element is not at the head, so search recursively in the tail. *
% *
% Example usage: *
% ?- linear_search(3, [1, 2, 3, 4]). *
% true. *
% ?- linear_search(5, [1, 2, 3, 4]). *
% false. *
% *
%****************************************************************************************************
% -----------------------------------------Linear search for an element in a list---------------------------------------------
% Base case: Element found at the head of the list.
linear_search(Element, [Element|_]).
% Recursive case: Search in the tail of the list.
linear_search(Element, [_|Tail]) :-
linear_search(Element, Tail).