Prolog flatten list problem?

There are some errors in your definition of flatten/2.

There are some errors in your definition of flatten/2: Your first clause will fail because if A is a list it will first instantiate R1 with R and then you try to unify it again with flatten(B, R1). Flatten(X,X). -> This clause leaves the list 'as is' without any flattening.

Check this other implementation: flatten(List, Flattened):- flatten(List, , Flattened). Flatten(, Flattened, Flattened). Flatten(Item|Tail, L, Flattened):- flatten(Item, L1, Flattened), flatten(Tail, L, L1).

Flatten(Item, Flattened, Item|Flattened):- \+ is_list(Item). Here we use two predicates: flatten/2 and flatten/3. The 'work' will be done in flatten/3 where the second argument will hold the intermediate flattened list.

The first clause is the base case: when we reach the empty list we are done so we instantiate the third argument with the intermediate flattened list. The second clause deals with recursion. It flattens the first item in the list (whether it is an item or a sublist), and proceeds with the rest of the input list.

The last clause is the 'base case' for the non-lists items. It prepends the item at the beginning of the intermediate flattened list, but it only does this for items which are not lists as that case was taken care in the second clause.

Thank you for the explanation. I had 1 day experience with prolog.. it is a little weird :) – Parhs Jun 20 at 16:04.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions