[erlang-questions] why is the the output not a list

Roelof Wobben r.wobben@REDACTED
Sat Sep 26 21:14:31 CEST 2015


Op 26-9-2015 om 21:10 schreef Stefan Schmiedl:
> Roelof Wobben (26.09. 20:08):
>
>> I think the next time I have to be more carefull and think longer about
>> what really needs to happen.
> I recommend writing a bit more about the code: Signature and purpose,
> for example just as comments:
>
>      -module(number_parser).
>      -export([scan/1]).
>
>      %% String -> ListOfTokens
>      %% produce a list of tokens (numbers and operators) from the given string
>      scan(String) -> lists:reverse(scan(String, [])).
>
>      %% String ListOfTokens -> ListOfTokens
>      %% accumulate the tokens in the second argument
>      scan([],           Parsed) -> Parsed;
>      scan([32|R],       Parsed) -> scan(R, Parsed);
>      scan([$+|R],       Parsed) -> scan(R, ['+' | Parsed]);
>      scan([H|_]=String, Parsed)
>        when H >= $0, H =< $9    -> digits(0, String, Parsed).
>
>      %% Number String ListOfTokens -> ListOfTokens
>      %% parse the leading number in given string and prepend its value to the list
>      digits(N, [], Parsed)      -> [N | Parsed];
>      digits(N, [H|R], Parsed)
>        when H >= $0, H =< $9    -> digits(N*10+(H-$0), R, Parsed);
>      digits(N, String, Parsed)  -> scan(String, [N | Parsed]).
>
> Or you could invest a bit of time to learn about the -spec directive,
> which would allow you to use dialyzer to discover certain types of errors:
>
>      -module(number_parser).
>      -export([scan/1]).
>
>      -type token() :: integer() | '+'.
>
>      -spec scan(string()) -> [token()].
>      scan(String) -> lists:reverse(scan(String, [])).
>
>      -spec scan(string(), [token()]) -> [token()].
>      scan([],           Parsed) -> Parsed;
>      scan([32|R],       Parsed) -> scan(R, Parsed);
>      scan([$+|R],       Parsed) -> scan(R, ['+' | Parsed]);
>      scan([H|_]=String, Parsed)
>        when H >= $0, H =< $9    -> digits(0, String, Parsed).
>
>      -spec digits(integer(), string(), [token()]) -> [token()].
>      digits(N, [], Parsed)      -> [N | Parsed];
>      digits(N, [H|R], Parsed)
>        when H >= $0, H =< $9    -> digits(N*10+(H-$0), R, Parsed);
>      digits(N, String, Parsed)  -> scan(String, [N | Parsed]).
>
> It's not really important what you use (for throwaway training projects),
> but it's a good habit to think about signature and purpose before implementing
> a function.
>
> If you want to know more about this approach, there's a three-part course
> on this topic over on edx.org, called Systematic Program Design. It does
> not use Erlang, but the ideas are still applicable in other environments.
> It does use a functional language (special teaching language derived from
> racket, i.e. scheme-like), so it's quite close to Erlang, actually :-)
>
> Best wishes,
> s.
>
>
> -----
> Geen virus gevonden in dit bericht.
> Gecontroleerd door AVG - www.avg.com
> Versie: 2015.0.6140 / Virusdatabase: 4419/10705 - datum van uitgifte: 09/26/15
>

Thanks,

Dialyzer is mentioned two chaptes later then this exercise.

Roelof





More information about the erlang-questions mailing list