I have the following list in prolog:
[[1,2],[2,3]]
Now, I can do a simple traversal in the list like this:
traverse([]).
traverse([H|T]):-
write(H),
traverse(T).
But, I don’t want this; I want to be able to access each element in the list as a list itself, not as a simple variable. What I want to do, out of this, is to cast each iteration of H into a list, so I can print out either the first or second value in H. However, I can’t come up with the syntax in prolog to do so. Can anyone help?
You need to unify (not cast) the H variable with the list constructor.
This disjunction (the two operands to the ; operator), is the same as what’s happening in the heads of the two clauses of traverse.
Therefore a separate predicate could also be used to traverse the list inside the list.