[erlang-questions] Silly question: if vs pattern matching

Matthias Lang matthias@REDACTED
Mon Feb 5 23:48:07 CET 2007


Joel Reymont writes:
 > process(String, Env) ->
 >      {Tree, _} = xmerl_scan:string(String),
 >      Closures = visit(Tree),
 >      Tree1 = render(Closures, Env),
 >      Tree2 = if
 >                  is_list(Tree1) ->
 >                      lists:flatten(Tree1);
 >                  true ->
 >                      [Tree1]
 >              end,
 >      XML = xmerl:export_simple(Tree2, xmerl_xml,
 >                                [#xmlAttribute{name = prolog,value =  
 > ""}]),
 >      lists:flatten(XML).
 > 
 > I don't particularly like the "if" above. It just doesn't seem very  
 > Erlangish to me and I would rather replace it with pattern matching.  
 > The silly question is...

How about just writing

  Tree2 = lists:flatten([Tree1]),

I won't speculate on the performance implications, but it eliminates
the if and saves five lines.

Matthias



More information about the erlang-questions mailing list