列表内的升序序数列表 [英] Prolog lists of ascending number inside a list
本文介绍了列表内的升序序数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在开始时有一个[1,2,3,1,0]列表,但需要将其拆分为多个子列表,新列表变为[[1,2,3],[1], [0]].
I Have a list of [1,2,3,1,0] at start but need to split it into a number of sub lists where the new lists becomes [[1,2,3],[1],[0]].
我在序言中知道的基本概念是通过比较数字.
The basic concept that I know in prolog is by comparing numbers.
ascending([Head | [HeadTail|TailTail]]) :- Head =< HeadTail.
推荐答案
我们可以使用基本列表的模式匹配
we can do with basic list' pattern matching
ascending([A], [[A]]).
ascending([A,B|T], R) :-
( A > B -> R = [[A],P|Q] ; P = [M|N], R = [[A,M|N]|Q] ),
ascending([B|T], [P|Q]).
测试:
1 ?- ascending([1,2],X).
X = [[1, 2]] ;
false.
2 ?- ascending([2,1],X).
X = [[2], [1]] ;
false.
3 ?- ascending([1,2,3,1,0],X).
X = [[1, 2, 3], [1], [0]] ;
false.
这篇关于列表内的升序序数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文