As @false already pointed out [ ]
is not a space but the empty list. Also your predicate is describing L
as Head
minus the empty list, and it doesn't care about the result of the recursion (deleteAll(Tail,_)
). That's why you get the unaltered first list as result.
Think about what the predicate should describe: a relation between two lists of lists where the second list contains the sublists of the first list without space, except the very last sublist, that is unaltered:
:- set_prolog_flag(double_quotes, chars).
lists_withoutspace([X],[X]). % last list unaltered
lists_withoutspace([H1,H2|T1],[H1WoS|T2]) :- % H1Wos:
list_withoutspace(H1,H1WoS), % first sublist without spaces
lists_withoutspace([H2|T1],T2). % the same for the rests
For list_withoutspace/2 you could use te built-in predicate char_type/2 to determine the type of the first list-element:
list_withoutspace([],[]). % empty list contains no space
list_withoutspace([X|T],L) :- % X is not in the list
char_type(X,space), % if it is space
list_withoutspace(T,L). % relation must also hold for tail
list_withoutspace([X|T],[X|L]) :- % X is in the list
char_type(X,alpha), % if it is a letter
list_withoutspace(T,L). % relation must also hold for tail
If you want to match more than letters change alpha
accordingly. If you query this predicate, you get the desired result:
?- lists_withoutspace([[q,' ',w,' ',e,' ',r,' ',t,' ',z],[a,' ',s,' ',d,' ',f,' ',g,' ',h],[y,' ',x,' ',c,' ',v,' ',b,' ',n]],L).
L = [[q,w,e,r,t,z],[a,s,d,f,g,h],[y,' ',x,' ',c,' ',v,' ',b,' ',n]] ? ;
no
Or more compactly:
?- lists_withoutspace(["q w e r t z","a s d f g h","y x c v b n"],L).
L = [[q,w,e,r,t,z],[a,s,d,f,g,h],[y,' ',x,' ',c,' ',v,' ',b,' ',n]] ? ;
no