John Doe
John Doe

Reputation: 35

Specify list length as a goal in Prolog

I want to use the built-in predicate include/3 to get all lists in a list of lists that have a length greater than N.

I've tried this:

% S is the size I'm checking for
include((length(_, N), N = S), List1, List2).

but it didn't work. I'm unsure how to specify my length goal correctly. I'm particularly unsure of what put in place of the '_'.

Upvotes: 1

Views: 62

Answers (1)

CapelliC
CapelliC

Reputation: 60034

The simpler way: provide a predicate with appropriate arguments (let's say, a schema adapter):

length_list(Len,List) :- length(List,Len).

and then

...,
include(length_list(S), List1, List2).

Depending on the system you're using, both yall and lambda libraries are good choices, worth to study.

Using yall:

?- L1=[[a],[a,b]],S=2,include({S}/[L]>>length(L,S),L1,R).
L1 = [[a], [a, b]],
S = 2,
R = [[a, b]].

Upvotes: 3

Related Questions