Would this predicate work ? this came on my quiz to find the position of element X in a data structure called list
Posted
by
M.K
on Stack Overflow
See other posts from Stack Overflow
or by M.K
Published on 2012-03-21T17:05:35Z
Indexed on
2012/03/21
17:29 UTC
Read the original article
Hit count: 166
example:
position(1,list(1,list(2,nil)),Z).
Z = 1.
position(3,list(1,list(2,list(3,nil)),Z).
Z = 3.
where Z is the position of element X in a data structure of a list in the above format
Here was my solution:
position(X,list(nil),0). %empty list
position(X,list(X,T),1). %list with X as head or first element
position(X,list(H,T),Z):-
position(X,list(T,nil),Z1), %X is in tail of list (H,T)
Z is Z1 + 1.
© Stack Overflow or respective owner