[erlang-questions] json to map

Roelof Wobben r.wobben@REDACTED
Fri Aug 28 13:04:14 CEST 2015


Op 28-8-2015 om 11:02 schreef Richard A. O'Keefe:
> On 28/08/2015, at 8:31 pm, Bengt Kleberg <bengt.kleberg@REDACTED> wrote:
>
>> On 08/28/2015 09:45 AM, Roelof Wobben wrote:
>>> I will take the challenge but im stuck at the types part.
> By far the easiest way to convert my Haskell sample code
> to Erlang is to throw the types completely away, or just
> leave them as comments.
>>> so far I have this :
>>>
>>> -module(time_parser).
>>>
>>> -export([]).
>>>
>>> -type token :: tInt()
>>>              | tWord()
>>>              | tSlash()
>>>              | tDash()
>>>              | tComma().
>>>
>>> -type tint()   :: integer().
>>> -type tword()  :: binary().
>>> -type tSlash() :: binary().
>>> -type tDash()  :: binary().
>>> -type tComma() :: binary().
> Leaving the omitted () aside, this isn't even CLOSE to a
> good translation of the Haskell data type.
> This makes tword -- should have been tWord and both of
> them should be t_word in idiomatic Erlang -- and tSlash
> and tDash and tComma the *same* type.  But the whole point
> is to make them DIFFERENT.

I have made them the same type because all three are only 1 character.
I have used the code from fiffy as reference to make my own.
I understand that they must be different but on some point they are the 
same.


> -type token
>     :: {int,integer()}
>      | {word,string()}  %% NOT binary!
>      | '/'
>      | '-'
>      | ','.
>
> The alternatives MUST be such that they cannot be
> confused with one another.
>
> tokens([])                           -> [];
> tokens([C|Cs]) when C =< 32          -> tokens(Cs);
> tokens([C|Cs]) when $0 =< C, C =< $9 -> digits(Cs, C-$0);
> tokens([C|Cs]) when $a =< C, C =< $z -> word(Cs, [C]);
> tokens([C|Cs]) when $A =< C, C =< $Z -> word(Cs, [C]);
> tokens("/"++Cs)                      -> ['/' | tokens(Cs)];
> tokens("-"++Cs)                      -> ['-' | tokens(Cs)];
> tokens(","++Cs)                      -> [',' | tokens(Cs)].
>


Pity , you gave me the answer. Now I can do a copy/paste and go on and 
the next time I do it wrong again.
That is why I want to try this one on my own and make my own mistakes 
and learn from it , make more mistake and also learn from them.






More information about the erlang-questions mailing list