prolog - How to check the type of an element in a list -
My list (which in turn is in lists) contains many different types of elements (obj1 (), Obj2 (), obj3 ..). I want to use each element in this list and only if I am currently using the element that is obj2 (_).
listObj = [[obj1 (_), obj2 (_), Obj1 (_)], [obj3, obj3, obj2 (_), obj1 (_)]], foreach (( X, list oz.), %% x x oz 2 (_) - & gt; Using the value of DoSomething X, %%, otherwise leave this element).
The use of lists of lists usually indicates a false representation of several systems lists But provide a
flatten / 2
library that may be useful, however, if you can not escape from this representation assuming that the list of lists in your example is flattened, you To run on the list, a simple recurrence can undoubtedly be written and your They can process each criterion that satisfies your criteria. Something like this:process ([]). Process ([element | elements]): - (Element = Obj.J. 2 (ARG) -> Dow_summing (ARG); true), process (element).
The description, however, will depend on many factors that you do not specify. For example, what does this mean if the processing of any
obj2 (_)
element fails? Failure in listing processing (as described above) continue on next element? List elements are free or can you share variables? Some prolol systems also predictforeach / 2
andforall / 2
which can be useful for compact (but not necessarily more efficient) solutions.
Comments
Post a Comment