Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have just started learning erlang...I am just practicing the recursion and all in erlang...I tried to write a few of the list processing functions on my own in erlang...Can someone review the code and give some advice on it...

-module(listma).
-export([duplicates/1,reverse/1])
%% Remove the duplicates in a list

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

duplicates([Head|Tail],Newlist) ->
    Insert = 
        fun(H,N) ->  
            case [H] -- N of
                [] -> N;
                [H] -> [H|N]
            end
        end,
    duplicates(Tail,Insert(Head,Newlist));

duplicates([],Outputlist) ->
    reverse(Outputlist).

%% Reverse a list

reverse(List) ->
    reverse(List,[]).
reverse([Head|Tail],Newlist) ->
    reverse(Tail,[Head|Newlist]);
reverse([],Outputlist) ->
    Outputlist.
share|improve this question

1 Answer 1

Your Insert lambda doesn't use any context so you could rewrite it as a top-level function or move its body to the clause. And you could use lists:member/2 instead of --.

I'd write first clause like this:

duplicates([Head | Tail], Acc) ->
   NewAcc = case lists:member(Head, Acc) of
               true ->
                  Acc;
               _ ->
                  [Head | Acc]
            end,
   duplicates(Tail, NewAcc);
share|improve this answer
    
Thanks for the feedback Dmitry...Thank you so much...The Insert Lambda is useless here as you said...I will remove it...I wanted to practice lists processing functions without the use of lists Built in functions like member,delete,append,etc..Thats why i ignored lists:member thing and used -- operator....Thanks again Dmitry...Thank you so much –  user23980 Apr 10 '13 at 12:30
    
Well, you can write your own member function too without use of -- =) –  Dmitry Belyaev Apr 10 '13 at 12:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.