1

I am trying to compute a list that contains the length of each list that are inside a list.

My code is:

length_list(A, B, N) :-
  findall(length(Path,E), 
  path(A, B, Path, Length), N).

path(A, B, [A, B], X) :-
  edge(A, B, X).

path(A, B, PathAB, Length) :-
  edge(A, C, X),
  path(C, B, PathCB, LengthCB),
  PathAB = [A | PathCB],
  Length is X + LengthCB.

path(A, B, PathAB, Length) will compute all paths that exist between the point A and the point B and its length

My problem is that my code returns:

?- length_list(a,d,N).
N = [length([a, b, d], _6624), 
     length([a, b, c, d], _6588), 
     length([a, b, c, e, d], _6546), 
     length([a, b, c, e|...], _6498), 
     length([a, b, c|...], _6456)].

It returns a list of length(Path,E) but I expected a list of E.
How can I do that?

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Gaelle Sou
  • 77
  • 1
  • 7
  • Yes sorry, I just found it here: [link](https://stackoverflow.com/questions/7436614/prolog-length-of-a-list) Thank you! – Gaelle Sou Dec 28 '18 at 18:00

0 Answers0