Prolog IntList 定义 [英] Prolog IntList definition

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

问题描述

hill(+IntList) 如果 IntList 由单调递增的 > 整数和后跟单调递减的整数组成,则成功.例如,>[1,2,5,8,11,6,3,-1] 是小山,但 [1,2,5,8,11,6,9,3,-1] 和 [1,2,3,4,5,6] >不是山丘.您可以假设 IntList 仅包含整数.

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] is a hill, but [1,2,5,8,11,6,9,3,-1] and [1,2,3,4,5,6] are >not hills. You may assume that IntList contains only integers.

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

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 定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆