list - Define predicate in prolog -


i'm struggling one:

define predicate len_nm(l,n,m) checks if list of lists l contains @ least n elements length no less m.

  1. the op stated:

    define predicate len_nm(l,n,m) checks if list of lists l contains at least n elements length no less m.

  2. in answer not solve original problem, following variation:

    define predicate len_nm(l,n,m) checks if list of lists l contains exactly n elements length no less m.

  3. similarly this answer, define 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. 
  4. in answer use :

     :- use_module(library(clpfd)). 

    then, define len_nm/4—using maplist/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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -