This is my first program in prolog, I've been reading about it and just don't seem to grasp a couple core concepts(I think). I'm trying to write a functor that will take two lists and return true only if the first list has more elements. I've gotten a few simple programs to work, but I have hit a roadblock here. I'm trying to call size inside of isLonger and set temp variables to the return of size. This seems like a bad(and incorrect) way of going about this in prolog. I'm getting a:
ERROR: >/2: Arguments are not sufficiently instantiated
% List 1
a([cat, dog, horse]).
b([1, 2, 3, 4]).
c([x, [a, b], y, z]).
c([red, yellow, green, blue]).
% isLonger function
isLonger([],[]).
isLonger(L1,L2) :- A = size(L1,N), B = size(L2,N), A > B.
% size([],N).
size([_|T],N) :- size(T,N1), N is N1+1.
Input:
isLonger([x,y,z], [7,8,9,10]).
A list L1 is longer than L2 if L1 is not empty and L2 is empty; or if the tail of L1 is longer than the tail of L2.
longer([_|_], []).
longer([_|T1], [_|T2]) :- longer(T1, T2).
Unfortunately, that solution would leave lost of choice points behind on several Prologs which can index predicates on the first argument only. That can be solved just reversing the argument order:
longer(L1, L2) :- shorter(L2, L1).
shorter([], [_|_]).
shorter([_|T1], [_|T2]) :- shorter(T1, T2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With