Erlang/OTP Forums

Author Message

<  Erlang  ~  Simple exercises

pellee
Posted: Mon Sep 24, 2007 10:45 am Reply with quote
Joined: 24 Sep 2007 Posts: 1
thanks michal!


Last edited by pellee on Mon Sep 24, 2007 11:48 am; edited 1 time in total
View user's profile Send private message
michal
Posted: Mon Sep 24, 2007 11:41 am Reply with quote
User Joined: 20 Jul 2006 Posts: 44 Location: London
In below examples H is for header, T for tail and A for accumulator.
Solution to B could look like this:
Code:

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

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


Solution to A could look like this:
Code:

filter(List, Integer) ->
    filter(List, Integer, []).

filter([H|T], I, A) when H =< I -> filter(T, I, [H|A]);
filter([_|T], I, A)             -> filter(T, I, A);
filter([], _, A)                -> lists:reverse(A).


Filtering could be also solved using list comprehension:
Code:

filter2(List, Integer) ->
    [Element || Element <- List,
                Element =< Integer].


Michal

_________________
http://www.erlang-consulting.com
View user's profile Send private message
francesco
Posted: Wed Sep 26, 2007 6:23 am Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
Why did you delete the original post? I am sure others would have found it interesting.

Francesco
--
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum