Prolog: How to make three lists the same lenght (by adding leading zeros)

Posted by sixtyfootersdude on Stack Overflow See other posts from Stack Overflow or by sixtyfootersdude
Published on 2010-06-05T20:52:02Z Indexed on 2010/06/06 0:22 UTC
Read the original article Hit count: 531

Filed under:
|

I am writing a prolog program to solve a problem. The problem takes three lists as input:

  solve( [L|Lr] , [R|Rr] , [S|Sr] ) :-

Unfortunately the lists all need to be equal length for the program to work.

So these work:

?- solve( [A, B, C, D] , [1, 3, 5, 6], [E, F, G, H]).
?- solve( [1] , [2], [3]).

But these do not:

?- solve( [A, B, C, D], [1], [B, I, T] ).
?- solve( [A], [1, 2], [4, 5]).

What I would like to do is write a predicate(?) to pad the smaller lists with leading zeros. So:

solve( [A, B, C, D], [1], [B, I, T] ) 

would become:

solve( [A, B, C, D], [0, 0, 0, 1], [0, B, I, T] )

Any points on how to accomplish this would be awesome. I am from a functional background so I am struggling. Is there a way tell the length of a list? I think I could do that recursively, but it seems like a pain.

Thanks

© Stack Overflow or respective owner

Related posts about prolog

Related posts about swi-prolog