Flatten a list in Prolog

后端 未结 7 698
执笔经年
执笔经年 2020-11-27 08:00

I\'ve only been working with Prolog for a couple days. I understand some things but this is really confusing me.

I\'m suppose to write a function that takes a list

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 08:24

    The definition of flatten2/2 you've given is busted; it actually behaves like this:

    ?- flatten2([a, [b,c], [[d],[],[e]]], R).
    R = [a, b, c] ;
    false. 
    

    So, given the case where you've already bound R to [a,b,c,d,e], the failure isn't surprising.

    Your definition is throwing away the tail of lists (ListTail) in the 3rd clause - this needs to be tidied up and connected back into the list to return via RetList. Here is a suggestion:

    flatten2([], []) :- !.
    flatten2([L|Ls], FlatL) :-
        !,
        flatten2(L, NewL),
        flatten2(Ls, NewLs),
        append(NewL, NewLs, FlatL).
    flatten2(L, [L]).
    

    This one recursively reduces all lists of lists into either single item lists [x], or empty lists [] which it throws away. Then, it accumulates and appends them all into one list again out the output.

    Note that, in most Prolog implementations, the empty list [] is an atom and a list, so the call to atom([]) and is_list([]) both evaluate to true; this won't help you throw away empty lists as opposed to character atoms.

提交回复
热议问题