Prolog IntList definition

后端 未结 3 1599
你的背包
你的背包 2021-01-21 06:26

hill(+IntList) succeeds if IntList consists of monotonically increasing >integers followed by monotonically decreasing integers. For example, >[1,2,5,8,11,6,3,-1

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-21 07:17

    Use clpfd!

    :- use_module(library(clpfd)).
    

    We don't need to worry about getting recursion right if we use append/3 and chain/2 like this:

    hill(Zs) :-
       Ascending0 =   [_|_],
       Descending = [M,_|_],
       append(Ascending0,Descending,Zs),
       append(Ascending0,[M],Ascending),
       chain(Ascending ,#<),
       chain(Descending,#>).
    

    Let's run the queries the OP gave!

    ?- hill([1,2,5,8,11,6,3,-1]).
      true                               % as expected
    ; false.
    
    ?- hill([1,2,5,8,11,6,9,3,-1]).
    false.                               % as expected
    
    ?- hill([1,2,3,4,5,6]).
    false.                               % as expected 
    

提交回复
热议问题