bug or feature?

Serge Aleynikov <>
Mon Apr 24 06:42:08 CEST 2006


Richard A. O'Keefe wrote:
[...]
> No, from the sound of it, the approach you tried is NOT "exactly the
> approach" that I'm talking about but very different indeed.  The approach
> I am talking about would NOT use fun(Prefix++Suffix, Prefix)  -- in which
> I for one would expect the first occurrence of Prefix to count as NOT BOUND.
[...]
> Let's take an example.  Suppose the prefixes are
>     http:
>     https:
>     ftp:
>     tftp:
[...]
> Ideally, if you wrote
> 
>     f("http:"  ++ S) -> S;
>     f("https:" ++ S) -> S;
>     f("ftp:"   ++ S) -> S;
>     f("tftp:"  ++ S) -> S;
>     f(            S) -> S.
[...]
> the compiler would generate essentially the same trie.  Certainly that's
> what I would expect a Haskell or SML compiler to do.  I don't know what
> the current Erlang compiler will do with it, which is why I mention the
> possibility of generating your own trie code.  It's really easy to do,
> once you have got your head around how to represent Erlang code as data
> structures.

Well, this all seems clear indeed if you know the exact values of 
Prefixes at compile-time, so that you can construct a pattern match such as:

f("http:" ++ S) -> S;

Perhaps I am missing your point, but if you have to obtain the prefix 
list from an external source at run-time, such as:

% get_prefixes_to_be_stripped() -> PrefixList
%   PrefixList = [ Prefix ]
%   Prefix     = string()

then the only way to use your recommendation would be to construct a 
string, such as:

"fun" ++ lists:flatten(
            ["(" ++ Prefix ++ "++ S) -> S;"
             || Prefix <- get_prefixes_to_be_stripped()])
  ++ "(S) -> S."

and then use erl_parse module to parse this string into a fun callable 
at run-time.  Correct?

It seems though that the "fun(Prefix++Suffix, Prefix)" notation would be 
a little bit less obscure, though, admittedly less efficient.

Regards,

Serge



More information about the erlang-questions mailing list