Exercise 3-5: Manipulating Lists

Next installment in my “Thomas does your homework” series…

[erlang]
-module(list).
-export([filter/2, reverse/1, concatenate/1, flatten/1]).

filter([Head|Tail],Num) when Head =< Num ->
[Head|filter(Tail,Num)];
filter([Head|Tail],Num) when Head > Num ->
filter(Tail,Num);
filter([],_) ->
[].

reverse([Head|Tail]) ->
reverse(Tail) ++ [Head];
reverse([]) ->
[].

concatenate([Head|Tail]) ->
Head ++ concatenate(Tail);
concatenate([]) ->
[].

% Probably possible to make this a LOT more efficient, ne
flatten([Head|Tail]) ->
flatten(Head) ++ flatten(Tail);
flatten([]) ->
[];
flatten(NotArry) ->
[NotArry].
[/erlang]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>