Take elements from one list and append to other
I want a function that will take two lists A and B and return lists Aout and Bout, such that elements from the beginning of A up to a given element (say the atom 'a') have been removed and appended to the end of B, discarding the character. My attempt below:
% usage: take_while(A, Aout, B, Bout)
take_while([], [], B, B).
take_while(['a'|As], As, B, B).
take_while([A|As], As, B, Bout) :-
append(B, [A], Bout),
%take_while(???
The last clause might be the 开发者_如何学编程wrong approach. How do I do this?
Looks like you need to simply add the call to take_while to that last clause: (Actually, I am not sure why the second parameter is needed so I'm removing it from this answer).
take_while([], [], B, B).
take_while(['a'|As], As, B, B).
take_while([A|As], ARem, B, Bout) :-
append(B, [A], BTemp), take_while(As, ARem, BTemp, Bout).
精彩评论