|
|
| Author |
Message |
< Erlang ~ Simple exercises |
| pellee |
Posted: Mon Sep 24, 2007 10:45 am |
|
|
|
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 |
|
| Back to top |
|
| michal |
Posted: Mon Sep 24, 2007 11:41 am |
|
|
|
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 |
|
| Back to top |
|
| francesco |
Posted: Wed Sep 26, 2007 6:23 am |
|
|
|
User
Joined: 07 Jul 2006
Posts: 249
Location: London
|
|
| Back to top |
|
|
|
All times are GMT
|
|
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
|
|
|