2010/4/20 vinu <
vinu...@gmail.com>:
> I tried this problem but it seems that I am missing something
> fundamental, I rad the text again and could not find anything
> consider this
>
> reverse([]) ->
> [];
> reverse([H|T]) ->
> [reverse(T)|H].
>
>
> it takes a list and reverse it but for following
> 1> reverse([1,2,3])
> prints
> [[[[]|3]|2]|1]
>
> I wonder why | did-not get processed.
Check you recursive case: [reverse(T)|H]. What does that mean?
Construct a list, with last element H and insert the result of
reverse(T) at the beginning. The tail of the list is [H], while the
head is reverse(T).
One way to do it is like this:
reverse([]) -> [];
reverse([H|T]) -> reverse(T) ++ [H].
The other would be using accumulators like many have shown you before.
--
Ale.