0

It should be done in a left-recursive way. Right now we have this:

listsFromList([],[]) .
listsFromList([Head|Tail], LL):-
   listsFromList(Tail,LL),
   is_list(Head),
   append(Head,LL, Newlist), LL is Newlist.
listsFromList([Head|Tail], LL):-
   listsFromList(Tail,LL),
   not(is_list(Head)), LL is LL.

However it keeps giving me this error:

ERROR: Type error: `[]' expected, found `[a,b]' (a list) ("x" must hold one character)

For example if I would query like this. The output should be like this:

?- listsFromList([1,[],2,3,4,[a,b]],X).
X = [[], [a, b]] .

Could someone please explain to me what I am doing wrong?

false
  • 10,264
  • 13
  • 101
  • 209

1 Answers1

0

You can use is_list:-

list([],[]).
list([H|T],[H|B]):-
    is_list(H),
    list(T,B).
list([H|T],B):-
    \+is_list(H),
    list(T,B).

?-list([1,[],2,3,4,[a,b]],X).
X = [[], [a, b]]
Reema Q Khan
  • 878
  • 1
  • 7
  • 20