[erlang-questions] newbie question

kab <>
Sat Nov 15 15:07:23 CET 2014


resending

I'm a noob trying to learn erlang too. I took Charles script and
applied the feedback. The only part I'm not grokking is why re:split
wants a 3 part list on the left hand side. I expected it to want two
parts; one for the string matched and the second for the remaining
string. Is there something about the re that is matching an empty
string?


-module(chunks).

main(_) ->
    io:format("Chunks Forward~n"),
    CF = chunks_fwd("Able was I ere I saw Elba."),
    lists:foreach(fun(A) -> io:format("~p~n", [A]) end, CF),
    io:format("Chunks Reverse~n"),
    CR = chunks_rev("Able was I ere I saw Elba."),
    lists:foreach(fun(A) -> io:format("~p~n", [A]) end, CR)
    .

chunks_fwd("") -> [];
chunks_fwd(Str)->
    P1 = "^([[:alnum:]]+|[[:space:]]+|[[:punct:]]+|[[:cntrl:]]+|.)",
    [[], M1, T1] = re:split(Str, P1, [{return, list}]),
    [M1 | chunks_fwd(T1)]
    .

chunks_rev(Str) ->
    chunks_rev(Str, []).
chunks_rev("", Lst) -> Lst;
chunks_rev(Str, Lst)->
    P1 = "^([[:alnum:]]+|[[:space:]]+|[[:punct:]]+|[[:cntrl:]]+|.)",
    [[], M1, T1] = re:split(Str, P1, [{return, list}]),
    chunks_rev(T1, [M1|Lst])
    .


On Thu, Nov 06, 2014 at 08:58:35PM +0100, Erik Søe Sørensen wrote:
> Hi there -
> 
> Apart from the atom-vs-Variable issue, I think you got your recursion wrong.
> 
>   [M1 | chunks(T1, P1)]
> 
> doesn't make too much sense (that I can see) - especially the "recurse with
> P1 for Lst" part.
> 
> I think you mean either
> 
>   chunks(T1, [M1|Lst])
> 
> for a tail recursive loop, or
> 
>   chunks("") -> [];
> 
>   chunks(Str) -> ..., [M1 | chunks(T1)]
> 
> for "normal" recursion on the rest. At present, you code looks like a
> confusion of the two (apart from the Lst/P1 difference).
> 
> 
> Regards, and have fun on the learning curve,
> 
> Erik
> 
> 
> Den 06/11/2014 20.27 skrev "Fred Youhanaie" <>:
> 
> >
> > and of course the same goes for lst in chunks/2 :)
> >
> > On 06/11/14 19:09, James Aimonetti wrote:
> >
> >> -----BEGIN PGP SIGNED MESSAGE-----
> >> Hash: SHA1
> >>
> >> str should be Str. Variables start with a capital letter.
> >>
> >> Le 2014-11-06 10:56, Charles Hixson a écrit :
> >>
> >>> Given the routine: -module(chunks). -export([chunks/1]).
> >>> chunks(str)-> chunks(str, []).
> >>>
> >>> chunks("", lst)-> list:reverse(lst); chunks(str, lst)-> P1 =
> >>> "^([[:alnum:]]+|[[:space:]]+|[[:punct:]]+|[[:cntrl:]]+|.)", [[],
> >>> M1, T1] = re:split(str, P1, [{return, list}]), [M1 | chunks(T1,
> >>> P1)].
> >>>
> >>> and the shell: 47> c(chunks). {ok,chunks}
> >>>
> >>> Why do I get: 49> chunks:chunks("Able was I ere I saw Elba."). **
> >>> exception error: no function clause matching chunks:chunks("Able
> >>> was I ere I saw Elba.") (chunks.erl, line 15) (Well, forget the
> >>> like 15.  I took out a bunch of comments.)
> >>>
> >> _______________________________________________
> > erlang-questions mailing list
> > 
> > http://erlang.org/mailman/listinfo/erlang-questions
> >

> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions


-- 
Kevin Baker - PnTOps       irc: kbaker  phone: +1 978 392 2442


More information about the erlang-questions mailing list