[erlang-questions] What is allowed in a pattern in terms of string concatenation?

Reynaldo Baquerizo reynaldomic@REDACTED
Mon Jun 27 18:16:57 CEST 2011


On Mon, 27 Jun 2011 11:12:39 -0500
Reynaldo Baquerizo <reynaldomic@REDACTED> wrote:

> On Mon, 27 Jun 2011 11:52:44 -0400
> Daniel Dormont <dan@REDACTED> wrote:
> 
> > Suppose I want to test that a certain string String1 ends with a
> > certain other string String2. The best I could come up with is:
> > 
> > case string:substr(lists:reverse(String1), 1, length(String2)) of
> > 	String2 -> ok;
> > 	_ -> not_ok
> > end
> > 
> > I was thinking there must be a better way, but various attempts
> > along the lines of case String1 of
> > 		_ ++ String2 -> ok;
> > 
> > all returned "illegal pattern" errors. I couldn't find a clear
> > explanation online of when exactly ++ is allowed in a pattern and
> > when it's not, so I'm a bit stumped. Is there a better way?
> 
> I can think of one using pattern matching,
> 
> is_substring(Substr, Substr) ->
>     true;
> is_substring(String, Substr) ->
>     [_|T] = String,
>     p5(T, Substr).

Oops, that should be:

is_substring(String, Substr) ->
    [_|T] = String,
    is_substring(T, Substr).

--
Reynaldo



More information about the erlang-questions mailing list