Write a Prolog program to implement nth_element (N, L, X) where N is the desired position, L is a list and X represents the Nth element of L.

16.Write a Prolog program to implement nth_element (N, L, X) where N is the desired position, L is a list and X represents the Nth element of L.

%Write Prolog program to implement nth_element (N, L, X) not.

%  below nth-element function is not predefined you can use any name as function

nth_element(1,[H|T],H).
nth_element(N,[H|T],X):- N1 is N-1,nth_element(N1,T,X).


% Output


No comments:

Post a Comment

Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...