本文介绍了Prolog IntList定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止所做的:

This is what I have done so far:

hill(List) :-
    increasing(List), decreasing(List).

increasing([H|Tail]) :-
    sm(H,Tail),
    increasing(Tail).
increasing([]).


decreasing([H|Tail]) :-
    gr(H,Tail),
    decreasing(Tail).

decreasing([]).

hill([]).

gr(X,[H|Tail]) :- X>H.
gr(X,[]).

sm(X,[H|Tail]) :- X<H.  
sm(X,[]).  

但这不起作用.逻辑是:数字列表是hill,如果它是increasing,然后是decreasing.我怎么说呢该代码执行increasingdecreasing,但是任何列表都不能同时是increasingdecreasing.

But this doesn't work. The logic is: A list of numbers is hill IF it is increasing and then decreasing. How do I say that? This code does increasing and decreasing, but no list can be both increasing and decreasing.

有什么想法吗?

推荐答案

hill(L1) :- concatenate(L2,L3,L1), inc(L2), dec(L3).
dec([X|[Y|[]]]) :- X > Y.
dec([X|[Y|L]]) :- X > Y, dec([Y|L]).
inc([X|[Y|[]]]) :- Y > X.
inc([X|[Y|L]]) :- Y > X, inc([Y|L]).
concatenate([],L2,L2).
concatenate([X|L1],L2,[X|L3]) :- concatenate(L1,L2,L3).

这有效:)

这篇关于Prolog IntList定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 00:19