Prolog possible removal of elements in a list

前端 未结 2 962
暖寄归人
暖寄归人 2021-01-15 10:17

I have been asked to try to search for all possible outcomes of, removing any numbers from any single elements from a list.

For example, if I have a list X = [1,2,3]

2条回答
  •  长情又很酷
    2021-01-15 10:37

    Here's another solution using append/3. I'm thinking @gusbro's answer is more elegant and/or efficient. But it's an alternative:

    reduce(L, R) :-
        append(L1, [X|L2], L),
        (   X1 is X - 1,              % some element varies 1 to X-1
            between(1, X1, Y),
            append(L1, [Y|L2], R)
        ;   append(L1, L2, R)         % or the element is just removed
        ).
    

提交回复
热议问题