I've stuck with the following problem:
I want to build up a list one-by-one from head to tail. But I have a condition, which depends on the already inserted elements.
condition(Sofar, Element) :-
between(1, 150, Element2),
\+member(Element2, Sofar),
Element = Element2.
makelist(L) :- maplist(condition(L), L).
An important note: Sofar
must not contain NewElement
, what I just try to assert! This is the point, where all my tries failed, because, with maplist, there is a reference in the list which gets the value of NewElement
Of course I have more complicated conditions like this, but if I could solve this, then I could adapt it.
You are trying to map notions you have learned in command-oriented (a.k.a imperative) languages to Prolog. Names like
Sofar
ormakelist
imply that you are doing something step-by-step. Try to see all of this from another angle by describing how the list looks like:Or, using clpfd: