Would this predicate work ? this came on my quiz to find the position of element X in a data structure called list
- by M.K
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.