[Erlang Programming Book] Exercise 3-5 reversing

85 views
Skip to first unread message

vinu

unread,
Apr 20, 2010, 12:53:53 PM4/20/10
to Erlang Programming
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.

--
Erlang Programming Website:
http://www.erlangprogramming.org/

Subscription settings: http://groups.google.com/group/erlang-programming-book/subscribe?hl=en

Ale

unread,
Apr 20, 2010, 1:10:20 PM4/20/10
to erlang-prog...@googlegroups.com
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.

>
> --
> Erlang Programming Website:
> http://www.erlangprogramming.org/
>
> Subscription settings: http://groups.google.com/group/erlang-programming-book/subscribe?hl=en
>



--
Ale.

vinu

unread,
Apr 22, 2010, 3:31:03 PM4/22/10
to Erlang Programming
I did it your way of course and eventually found out my first solution
did not work because erlang doesnot usually flatten the list
automatically.

On Apr 20, 10:10 pm, Ale <peralta.alejan...@gmail.com> wrote:
> 2010/4/20 vinu <vinu76...@gmail.com>:

Rakesh Kumar

unread,
Oct 3, 2014, 9:00:43 PM10/3/14
to erlang-prog...@googlegroups.com, vinu...@gmail.com
reverse([]) -> [];
reverse([H|T]) -> reverse(T) ++ [H].

Hynek Vychodil

unread,
Oct 10, 2014, 4:30:11 AM10/10/14
to erlang-prog...@googlegroups.com
Your solution has O(n^2) complexity which is not good solution. It is why they stated there should be solved with auxiliary function:

reverse(L) -> reverse(L, []).

reverse([], L) -> L;
reverse([H|T], L) -> reverse(T, [H|L]).

--
--
Erlang Programming Website:
http://www.erlangprogramming.org/
---
You received this message because you are subscribed to the Google Groups "Erlang Programming" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erlang-programmin...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Francesco Cesarini

unread,
Oct 15, 2014, 6:34:24 AM10/15/14
to erlang-prog...@googlegroups.com
There is a side note in the book which explains why you should avoid List ++ [Element], especially in recursive calls. With the current implementation of the VM, to append Element to the end of the list, you need to traverse every element in List. Doing this for every iteration in your recursive call with have huge impact on performance.

F


Reply all
Reply to author
Forward
0 new messages