Seeking advice on port "pattern" (long)
Torbjorn Tornkvist
tobbe@REDACTED
Tue Jun 26 16:30:57 CEST 2001
> Can you provide an example of what you mean? I know Joe posted one some time
> ago but I can't find it and haven't quite managed to figure it out for
> myself.
Ok, below I've whipped up something to show what I meant.
Cheers /Tobbe
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(abc).
-export([abc/1]).
%%%
%%% Parse strings looking like: "abc"
%%% Example:
%%%
%%% 2> abc:abc("abcdefg").
%%% {ok,"abc","defg"}
%%% 3> {_,C} = abc:abc("a").
%%% {needmore,#Fun<abc.1.119051369>}
%%% 4> {_,C2} = C("b").
%%% {needmore,#Fun<abc.2.1309902>}
%%% 5> C("cdef").
%%% {parse_error,{"a",'<here>',"cdef"}}
%%% 6> C2("cdef").
%%% {ok,"abc","def"}
%%%
abc(In) ->
a(In,[]).
a([$a|T],Acc) -> b(T, [$a|Acc]);
a([],Acc) -> {needmore, fun(More) -> a(More, Acc) end};
a(In,Acc) -> parse_error(In,Acc).
b([$b|T],Acc) -> c(T, [$b|Acc]);
b([],Acc) -> {needmore, fun(More) -> b(More,Acc) end};
b(In,Acc) -> parse_error(In,Acc).
c([$c|T],Acc) -> {ok, lists:reverse([$c|Acc]), T};
c([],Acc) -> {needmore, fun(More) -> c(More, Acc) end};
c(In,Acc) -> parse_error(In,Acc).
parse_error(In,Acc) -> {parse_error, {lists:reverse(Acc), '<here>', In}}.
More information about the erlang-questions
mailing list