trying to flatten a list



Hello!
I am trying to flatten a list (making a list like [1,3,[4,5],6,9] to become [1,2,4,5,6,9].
So I created a predicate flatten/2, that expects the first argument to be the list to be flattened and the next argument to become a
variable.
It would in other ways be called like this:
flatten([1,3,[4,5],6,9],X).


Here is the program:

flatten([],[]).
flatten([[A]|B],[A|B]).
flatten([A|B],[A|C]) :- not(A = [_|_]), flatten(B,C).


It seems that I do not get a unification against the 2nd predicate if the first element of the list denoted by arg 1 is a list!
Why?

Do I need to write it in another way?

And what about the check if A is not a list in the predicate on line 3?
Is that appropriate?

best regards
Anders Lindén


.



Relevant Pages

  • Re: trying to flatten a list
    ... > So I created a predicate flatten/2, that expects the first argument to be the list to be flattened and the next argument to become a ... > It seems that I do not get a unification against the 2nd predicate if the first element of the list denoted by arg 1 is a list! ... Your second predicate only catches single-value lists. ...
    (comp.lang.prolog)
  • Re: How to stop infinite loop while doing basic I/O
    ... simple Prolog predicate using a cut in its definition. ... opinion only telling half of the story because the unfolded Prolog code ... with regards to ease of understanding. ...
    (comp.lang.prolog)
  • Re: Are Logtalk objects "real" objects?...
    ... One of the provided examples, "roots", shows how to use this predicate ... greatly improved by dropping features that you're not going to use and ... Best regards, ...
    (comp.lang.prolog)
  • Re: Are Logtalk objects "real" objects?...
    ... One of the provided examples, "roots", shows how to use this predicate ... allow static binding of its predicates. ... Best regards ...
    (comp.lang.prolog)
  • Re: Calling C code in prolog
    ... I finally got it working, now i have just one last issue, it regards ... the rename_file built in predicate. ... I am using open file to create a file by taking a user input and ...
    (comp.lang.prolog)