[erlang-questions] String Pattern Matching

Vance Shipley <>
Sun Sep 7 20:47:46 CEST 2008


On Sun, Sep 07, 2008 at 10:02:37AM -0400, Luke Galea wrote:
}  I get the sense that most erlangers feel that regular expressions  
}  aren't needed and that pattern matching can do the job.

Whenever possible.

}  	([m|l])ouse$ -> $1ice

-module(inflector).
-export([singularize/1]).

singularize(Word) ->
	L = lists:reverse(Word),
	lists:reverse(singularize1(L)).

singularize1("ecil" ++ L) ->
	"esuol" ++ L;
singularize1("ecim" ++ L) ->
	"esuom" ++ L.

1> inflector:singularize("titmice").
"titmouse"

If you're really just dealing in words the penalty of a double
reverse isn't as bad as the penalty of having to use REs.

	-Vance



More information about the erlang-questions mailing list