trying to flatten a list
- From: "Anders Lindén" <xxxx@xxxxxx>
- Date: Thu, 26 May 2005 23:54:03 +0200
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
.
- Follow-Ups:
- Re: trying to flatten a list
- From: student
- Re: trying to flatten a list
- From: Anders Lindén
- Re: trying to flatten a list
- From: Roy Haddad
- Re: trying to flatten a list
- Prev by Date: Re: Prolog [book] for beginners.
- Next by Date: Re: trying to flatten a list
- Previous by thread: interpreter?
- Next by thread: Re: trying to flatten a list
- Index(es):
Relevant Pages
|
|