list - Define predicate in prolog -
i'm struggling one:
define predicate
len_nm(l,n,m)
checks if list of listsl
contains @ leastn
elements length no lessm
.
the op stated:
define predicate
len_nm(l,n,m)
checks if list of listsl
contains at leastn
elements length no lessm
.in answer not solve original problem, following variation:
define predicate
len_nm(l,n,m)
checks if list of listsl
contains exactlyn
elements length no lessm
.similarly this answer, define dcg
seqq1//1
establish relationship between list of non-empty lists , flattened opposite:seq([]) --> []. seq([e|es]) --> [e], seq(es). seqq1([]) --> []. seqq1([es|ess]) --> {es=[_|_]}, seq(es), seqq1(ess).
sample use:
?- phrase(seqq1([[1,2],[3],[4]]),xs). xs = [1,2,3,4].
note
seqq1//1
works in "both directions":?- phrase(seqq1(xss),[1,2,3,4]). xss = [[1],[2],[3],[4]] ; xss = [[1],[2],[3,4]] ; xss = [[1],[2,3],[4]] ; xss = [[1],[2,3,4]] ; xss = [[1,2],[3],[4]] ; xss = [[1,2],[3,4]] ; xss = [[1,2,3],[4]] ; xss = [[1,2,3,4]] ; false.
in answer use clpfd:
:- use_module(library(clpfd)).
then, define
len_nm/4
—usingmaplist/3
,length/2
,tcount/3
, ,(#=<)/3
:len_nm(xss,ys,n,m) :- m #>= 1, n #>= 0, phrase(seqq1(xss),ys), maplist(length,xss,ls), tcount(#=<(m),ls,n).
let's run sample queries!
?- len_nm([[1,2,3],[4],[5,6],[7,8,9,10],[11,12]],_,n,l). n = 5, l = 1 % 5 lists have length of @ least 1 ; n = 4, l = 2 % 4 lists have length of @ least 2 ; n = 2, l = 3 % 2 of @ least 3 (e.g., [1,2,3] , [7,8,9,10]) ; n = 1, l = 4 % 1 list has length of 4 (or more) ; n = 0, l in 5..sup. % no list has length of 5 (or more)
ok! how one?
?- append(xs,_,[x,x,x,x,x,x]), % `xs` having @ 6 elements ... n #>= 1, % ... `xss` shall contain @ least 1 list ... len_nm(xss,xs,n,4). % ... having length of 4 (or more). xs = [x,x,x,x], n = 1, xss = [[x,x,x,x]] ; xs = [x,x,x,x,x], n = 1, xss = [[x],[x,x,x,x]] ; xs = [x,x,x,x,x], n = 1, xss = [[x,x,x,x],[x]] ; xs = [x,x,x,x,x], n = 1, xss = [[x,x,x,x,x]] ; xs = [x,x,x,x,x,x], n = 1, xss = [[x],[x],[x,x,x,x]] ; xs = [x,x,x,x,x,x], n = 1, xss = [[x],[x,x,x,x],[x]] ; xs = [x,x,x,x,x,x], n = 1, xss = [[x],[x,x,x,x,x]] ; xs = [x,x,x,x,x,x], n = 1, xss = [[x,x],[x,x,x,x]] ; xs = [x,x,x,x,x,x], n = 1, xss = [[x,x,x,x],[x],[x]] ; xs = [x,x,x,x,x,x], n = 1, xss = [[x,x,x,x],[x,x]] ; xs = [x,x,x,x,x,x], n = 1, xss = [[x,x,x,x,x],[x]] ; xs = [x,x,x,x,x,x], n = 1, xss = [[x,x,x,x,x,x]] ; false.
Comments
Post a Comment