1

Can you help me with this problem of backtracking in Prolog? The string a1, ..., an is given and it consists of distinct integers. It is required to display all sub-sets with aspect of "mountain" (a set has a "mountain" aspect if the elements increase to a certain point and then decrease). Eg. 10 16 27 18 14 7. I know how to verify if a set has mountain aspect... but now I need to display all solutions :(

% munte(list,integer,integer)
% rezolvare(list)

munte([],2,_).
munte([H|[]],2,_).
munte([H|[H1|T]],Pas,Nr):-
    Pas = 1,
    H<H1,!,
    L=[H1|T],
    Nr1=Nr+1,
    munte(L,Pas,Nr1).

munte([H|[H1|T]],Pas,Nr):-
    Pas = 1,
    H>H1,
    Nr>1,!,
    L=[H1|T],
    Pas1=Pas + 1,
    Nr1=Nr+1,
    munte(L,Pas1,Nr1).                       

munte([H|[H1|T]],Pas,Nr):-
    Pas = 2,
    H>H1,!,
    L=[H1|T],
    Nr1=Nr+1,
    munte(L,Pas,Nr1).

rezolvare(L1):-munte(L1,1,1).
repeat
  • 18,496
  • 4
  • 54
  • 166

1 Answers1

1

Note that this answer deals with subsequences of lists, not subsets of sets.

In the following we use :

:- use_module(library(clpfd)).

We define hillSubseq_in/2 based on tfilter/3, (*)/2 and hill/1:

hillSubseq_in(Zs,Xs) :-
   tfilter(*,Xs,Zs),
   hill(Zs).

Sample query:

?- hillSubseq_in(Zs,[2,9,3,1,5,9,2]).
  Zs = [2,9,3,1]
; Zs = [2,9,3,2]
; Zs = [2,9,3]
; Zs = [2,9,1]
; Zs = [2,9,5,2]
; Zs = [2,9,5]
; Zs = [2,9,2]
; Zs = [2,3,1]
; Zs = [2,3,5,9,2]
; Zs = [2,3,5,2]
; Zs = [2,3,9,2]
; Zs = [2,3,2]
; Zs = [2,5,9,2]
; Zs = [2,5,2]
; Zs = [2,9,2]
; Zs = [3,5,9,2]
; Zs = [3,5,2]
; Zs = [3,9,2]
; Zs = [1,5,9,2]
; Zs = [1,5,2]
; Zs = [1,9,2]
; Zs = [5,9,2]
; false.
Community
  • 1
  • 1
repeat
  • 18,496
  • 4
  • 54
  • 166