1

I'm currently writing a predicate that will run through a list of lists and insert a value I have calculated onto the beginning of the list

Step one is easy, just perform the calculation for each list and unify variable N with it.

checkthrough([]).

checkthrough([H|T]):-
    count_validentries(H,N),
    checkthrough(T).

What I'm trying to achieve now is to put that variable N onto the beginning of each of my sublists, so each list begins with the count of valid entries.

I have attempted to do this using an accumulator. Attempting to start with an empty list, and to every time add the new value N and the head of the list to it:

checkthrough([],Sofar,Lastone).

checkthrough([H|T],Sofar,Lastone):-
    count_validentries(H,N),
    Newsofar is [N,H|Sofar],
    checkthrough(T,Newsofar,Lastone).

I'm quite sure I'm making a really stupid mistake somewhere along the lines. This is not valid Prolog syntax, failing with Arithmetic:' [2 internal variables]' is not a function.

Does anyone have any tips please?

John
  • 303
  • 2
  • 12

2 Answers2

4

Using maplist/3 and Prolog simply write:

?- use_module(library(lambda)).

?- maplist(\Es^[N|Es]^count_validentries(Es,N), Ess, Xss).

Also, I'd guess that you're really looking for (-)/2 pairs which is how key-value pairs are commonly represented—by library predicates and the built-in predicate keysort/2. Consider:

?- Ess = [[a,b,c],[d,e],[],[f]],
   maplist(\Es^(N-Es)^length(Es,N), Ess, Xss), 
   keysort(Xss, Yss).
Ess = [  [a,b,c],   [d,e],   [],   [f]],
Xss = [3-[a,b,c], 2-[d,e], 0-[], 1-[f]],
Yss = [0-[], 1-[f], 2-[d,e], 3-[a,b,c]].
Community
  • 1
  • 1
repeat
  • 18,496
  • 4
  • 54
  • 166
2

Maybe

checkthrough([],Sofar,Sofar).
checkthrough([H|T],Sofar,Lastone):-
  count_validentries(H,N),
  checkthrough(T,[[N|H]|Sofar],Lastone).

but you'll end up with the list reversed. Keeping it simpler will help

checkthrough([],[]).
checkthrough([H|T],[[N|H]|Rest]):-
  count_validentries(H,N),
  checkthrough(T,Rest).

or better, if you're running a recent version of SWI-Prolog:

checkthrough(L,L1) :-
   maplist([E,E1]>>(count_validentries(E,N),E1=[N|E]), L,L1).
CapelliC
  • 59,646
  • 5
  • 47
  • 90