João Costa
João Costa

Reputation: 412

Prolog Lists with Lists inside

I'm a total newbie in Prolog I have this code (I'm sorry if its not very friendly to read)

AdjOfAll(State,[FreeSpace],[Result]):-
     Adj(State,FreeSpace,Result).
AdjOfAll(State,[Space|NextSpace],[X|Xs]):-
     AdjOfAll(State,NextSpace,Xs),
     Adj(State,Space,X).

my problem is that Adj returns [ (element_1), ... , (element_n) ]

My objective with AdjOfAll was to get,

[ ( element_1), ..., (element_n), (element_1_from 2nd adj), ... ]

but i get

[ [ ( element_1), ... ,(element_n)] , [ (element_1_from 2nd adj) ], ... ]

I hope its understandable what I'm trying to say, sorry for the bad English

Upvotes: 1

Views: 138

Answers (1)

CapelliC
CapelliC

Reputation: 60034

see if flatten/2 can help you to get the right output. Place it after the top level call to AdjOfAll/3.

...
AdjOfAll(State, NextSpace, ResultNested),
flatten(ResultNested, Result),
...

BTW are you sure your predicates are named correctly? Usually they are lowercase.

Upvotes: 1

Related Questions