Re: trying to flatten a list



Anders Lindén wrote:
> 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?

Your second predicate only catches single-value lists. That is, it only
catches things like [1,3,[4],6,...], but not [1,3,[4,5],6,...] or
[1,3,[],6,...].

Try starting the second predicate like this:

flatten([[A|B]|C], [X|Y]) :- ...

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

First of all, the check is not equivalent to the statement "the first
argument of this predicate would not match in the second predicate",
which seems to have been your intention. It should have been "not(A =
[_])", if it was, but neither of those are efficient. You can get the
same behavior with a cut:

flatten([[A]|B],[A|B]) :- !.
flatten([A|B],[A|C]) :- flatten(B,C).

This forces Prolog to not try any other alternatives once it has found a
match.
.



Relevant Pages

  • Re: An instance of Russells paradox?
    ... >>infinite generalization of the arity of every predicate (I got this ... the representation ... > of lists is actually syntactic sugar for the binary term ... notation, the list notation, and the operator notation. ...
    (sci.logic)
  • Re: pythonic filter function?
    ... ((predicate (car list)) ... (filter predicate (cdr list))))) ... I would like to note that the very best thing to do is to avoid writing code that conses lists. ... you should use standard parts of Common Lisp that do the consing for you. ...
    (comp.lang.scheme)
  • Re: Prolog syntax
    ... is the notation for lists, so is a list of length one containing only the variable TYPE. ... decomposes a term into a list with its first element the predicate name followed by the predicate's arguments. ... in your example TYPE contains the predicate name to be called, so Cat becomes the predicate starting with TYPE and a single parameter SS, in the last line "Cat.", the just built predicate is called. ... is an infix defined function symbol and originally used to build arithmetic expressions. ...
    (comp.lang.prolog)
  • Re: Applying Godel
    ... Godel sentence for 1 particular theory T, ... The proof predicate needs to be binary and primitive recursive. ... you have to encode Both sentences AND lists-of-sentences ...
    (sci.logic)
  • Re: xsl -- using math in element subscripts?
    ... What you call a subscript is actually not. ... It is a predicate. ... merging preserves the doc order and does not duplicate any nodes in both lists. ... Notice that the name of the age group on the first vehicle ...
    (comp.text.xml)