Reputation: 73
this is my first question here, I'm new in prolog and I'm doing some homework for school.
I have to make a list of lists and I was fighting with these for 2 days now. Maybe some of you can help me.
This is what I have to do:
With an entry of some thing like this:
evaluateList([f(14),_,_,_,_,f(31),_,_],E)
E should be:
E = [14,[_,_,_,_],31,[_,_]]
I have tried to do it, but I cant get the expected result:
evaluateList([],[]).
evaluateList([H|T], [S]) :- var(H), do_vars([H|T],S), !.
evaluateList([f(N)|T],[N|S]) :- evaluateList(T,S), !.
do_vars([],[]).
do_vars([H|T],[H|S]) :- var(H), do_vars(T,S).
After running the code:
evaluateList([f(14),_,_,_,_,f(31),_,_],E)
I'm getting:
E = [14, _1378, _1384, _1390, _1396, 31, [_1414, _1420]]
I mean, I'm getting the 'inside list', but only in the last item.
Any suggestion?
Thank you
Upvotes: 1
Views: 55
Reputation: 60034
do_vars
should 'eat' elements from the list, and return the remainder. So, an additional argument is needed. For instance
evaluateList([],[]).
evaluateList(L,[Vs|T]) :- do_vars(L,Vs,R), Vs\=[], !, evaluateList(R,T).
evaluateList([f(N)|T],[N|S]) :- evaluateList(T,S).
do_vars([V|L],[V|S],R) :- var(V), do_vars(L,S,R).
do_vars(R,[],R).
Upvotes: 1