Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List to List of Lists

Status
Not open for further replies.

RibasMan

Programmer
Oct 4, 2008
2
PT
Hi Guys,

I'm new at prolog, and for now i'm yet learning on how to do this kind of programming.

The problem is: I have a list with number that are for a game. That game is a board game and always with equal lines/cols like 2x2 ,3x3, 10x10, etc...

I have created a single list to have those numbers; now i would like to have that list separated in a list of lists this way: if i have a 10x10 board i want 10 lists of 10 numbers each inside a list, and if it is 3x3 i want 3 lists of 3 numbers each inside a list, and so on...

For example:
a 3x3 board
L = [4 , 5, 6, 4, 1 , 3, 5, 6, 8]
Should be
L = [[4,5,6],[4,1,3],[5,6,8]]


 
I think that this kind of thing is pretty hard to do and to understand for a beginner.

We have the basic prédicate
lst_2_lst_of_lst(+L, +N, -LL).
+L the list t convert
+N the number for the groupings.
-LL the final list

We have the helper predicate

lst_2_lst_of_lst_helper(L, N1, N, LC)
L the rest of the list to convert
N1 the current number of grouping
N the number of groupings
LC list in construction


We launch the computation with

Code:
lst_2_lst_of_lst(L, N, LL) :-
	lst_2_lst_of_lst_helper(L, 1, N, LL).


We will go through the list in recursion and we construct the result at the exit of the recursion.

so the first clause is

We have nothing left to group, so the result is empty [].
Code:
lst_2_lst_of_lst([], _N, []).

Code:
lst_2_lst_of_lst_helper([H|T], N, N, [[H]|LL]):-
	lst_2_lst_of_lst(T, N, LL).
This is the trick of the computation.
When N1 = N, we memorize the element H in a list that we add to the list LL that we will built in the continuation of the recursion.

Code:
lst_2_lst_of_lst_helper([H|T], N1 , N, [[H|T1]|LL]):-
	N2 is N1 + 1,
	lst_2_lst_of_lst_helper(T, N2 , N, [T1| LL]).
We construct the current sublist, it's the same that the above clause. The element H is added to the list that we be build later.

It is an usual way of proceeding in Prolog.

Hope this help !


The complete code
Code:
lst_2_lst_of_lst([], _N, []).

lst_2_lst_of_lst(L, N, LL) :-
	lst_2_lst_of_lst_helper(L, 1, N, LL).

lst_2_lst_of_lst_helper([H|T], N, N, [[H]|LL]):-
	lst_2_lst_of_lst(T, N, LL). 

lst_2_lst_of_lst_helper([H|T], N1 , N, [[H|TMP]|LL]):-
	N2 is N1 + 1,
	lst_2_lst_of_lst_helper(T, N2 , N, [TMP| LL]).
 
Yeah man its kind of hard, but i have to take some time to understand this better;

I do programming for many years and now i can say that i am good at many languages like C,C++,Java, etc; but this year i'm learning prolog and this is a total new concept for me, it's like i know how to do the things but i can't translate the ideas to prolog, and that's it's some times boring...

But as always with time and study i will get there.

Many Thanks joel.
 
I practiced C for a long time, and I must say that Prolog and Scheme "opened my eyes".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top