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

Roelof Wobben r.wobben@REDACTED
Sat Sep 26 17:47:22 CEST 2015


Op 26-9-2015 om 15:36 schreef Stefan Schmiedl:
> Roelof Wobben (26.09. 14:44):
>
>> Hello,
>>
>> I tried to make a number parser which can parse numbers above the 10.
>>
>> So I change my code to this :
>>
>> -module(number_parser).
>>
>> -export([scan/1]).
>>
>> scan(String) ->
>>       scan(String, []).
>>
>> scan([], List) ->
>>       List;
>>
>> scan([Head | Rest], List_parse) when Head >= $0, Head =< $9 ->
>>        digits(Head, Rest, List_parse);
>>
>> scan( [$+ | Rest], List_parse) ->
>>      scan(Rest, [$+ | List_parse] );
>>
>> scan([32 | Rest], List_parse) ->
>>       scan(Rest, List_parse).
>>
>>
>> digits(Number, [Head | Rest] , List_parse ) when Rest >= $0 , Head =< 10 ->
>                                                     ^^^^ Head
>
> conditions after a "when" fail silently, so you're never going into
> this function
>
> s.
>
>>       digits(Number + Head, Rest, List_parse);
>>
>> digits(Number, String , List_parse ) ->
>>       scan(String, [Number | List_parse]).
>>
>> But when I do number_parser:scan("1 + 1") I see this as output "1+1"
>> where I was expecting to see [ [1] . [+],[1]]
>> Where did my thinking took the wrong path.
>>
>> Roelof
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>
> -----
> Geen virus gevonden in dit bericht.
> Gecontroleerd door AVG - www.avg.com
> Versie: 2015.0.6140 / Virusdatabase: 4419/10702 - datum van uitgifte: 09/26/15
>
Thanks ,

Still something is not working as expected and I do not see what it is,

I have this code now :

-module(number_parser).

-export([scan/1]).

scan(String) ->
     scan(String, []).

scan([], List) ->
     List;

scan([Head | Rest], List_parse) when Head >= $0, Head =< $9 ->
      digits(Head, Rest, List_parse);

scan( [$+ | Rest], List_parse) ->
    scan(Rest, [$+ | List_parse] );

scan([32 | Rest], List_parse) ->
     scan(Rest, List_parse).


digits(Number, [Head | Rest] , List_parse ) when Head >= $0 , Head =< $9 ->
    io:format("Number: ~w", [Number]),
    io:format("Head: ~w", [Head] ),
    New_number = (Number - 48) * 10 + Head - 48,
    digits(New_number, Rest, List_parse);

digits(Number, String , List_parse ) ->
    io:format("Number: ~w ~n",  [Number - 48]),
    io:format("String: ~s ~n", [String]),
    io:format("List_parse: ~s ~n", [List_parse]),
    scan(String, [Number | List_parse]).

When I do number_parser("1+1") I see this output:

Number: 1
String: + 1
List_parse:

Number: 1
String:
List_parse: +1

So still no list and the first 1 disappear.

When I do number_parser:scan("10+1") I see this output :

Number: 49
Head: 48
Number: -38
String: + 1
List_parse:

Number: 1
String:
List_parse: +

So all numbers has disapear.

Roelof




More information about the erlang-questions mailing list