Build up a list, but mind the previous elements

2019-07-27 01:41发布

问题:

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.

回答1:

You are trying to map notions you have learned in command-oriented (a.k.a imperative) languages to Prolog. Names like Sofar or makelist 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:

A list of elements between 1 and 150 that are all different.

all_dif([]).
all_dif([E|Es]) :-
   maplist(dif(E), Es),
   all_dif(Es).

speciallist(Es) :-
   all_dif(Es),
   maplist(between(1,150), Es).

Or, using clpfd:

:- use_module(library(clpfd)).

specialfdlist(Es) :-
   Es ins 1..150,
   all_different(Es).


标签: list prolog