[erlang-questions] Is this code tail recursive ?

Robert Virding robert.virding@REDACTED
Mon Feb 7 16:24:04 CET 2011


Yes, it is tail-recursive in gather_files/1/2. Though it will probably not return what you want as the first clause of gather_files/2:

gather_files([], [Acc]) -> Acc;

will only succeed if there is ONE element (file) in the accumulator in which case it will return the file, otherwise it will crash. Did you intend

gather_files([], Acc) -> Acc; ?

Robert

----- "Mihai Balea" <mihai@REDACTED> wrote:

> On Feb 6, 2011, at 6:08 PM, Radu Brumariu wrote:
> 
> > gather_files([]) -> [];
> > gather_files(F) ->
> >    gather_files(F,[]).
> > gather_files([],[Acc]) -> Acc;
> > gather_files([{path,F}|Files],Acc) ->
> >    io:format("Processing path ~p~n",[F]),
> >    {ok, T} = file:read_file_info(F),
> >    case T#file_info.type
> >    of
> > 	regular ->
> > 	    gather_files(Files, [F | Acc]);
> > 	directory ->
> > 	    gather_files(Files, [gather_directory_files(F) | Acc ]);
> > 	_ ->
> > 	    gather_files(Files, Acc)
> >    end.
> > 
> > gather_directory_files(D) ->
> >    filelib:fold_files( D, ".*", true, fun(X,Acc) ->
> > 				        [X|Acc]
> > 			       end,
> > 		       []).
> > 
> 
> Looks tail recursive to me, although looking at the way you build up
> your result, it might not be what you expect :) 
> Not sure what you expect though, so I cannot comment on that.
> 
> Hope this helps,
> Mihai
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED

-- 
Robert Virding, Erlang Solutions Ltd.



More information about the erlang-questions mailing list