[erlang-questions] json to map

Garrett Smith g@REDACTED
Fri Aug 28 13:23:33 CEST 2015


On Fri, Aug 28, 2015 at 6:04 AM, Roelof Wobben <r.wobben@REDACTED> wrote:
> 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.

No no no no. That's not the way to think about this IMO.

In software you learn from your failures, but you learn *a lot more*
from others' successes!

I had originally suggested that you start learning about parser
generating by giving you a complete working example, albeit a simple
one. That's not depriving you of learning, it's giving you a road map
for learning. With a working model you can start to tinker with the
code, *use it*, start to change little bits of it to experiment with
it and see how things change. IMO that's a learning process, it
doesn't give anything away or make anything less valuable - to the
contrary.

I haven't read this thread carefully enough to know what this new
challenge is, but I'd go back to the challenge I suggested earlier,
which is to create a simple calculator. Use the hint - just use it!
(Maybe you have already and have moved on to your next challenge -
again, I'm not reading carefully here.) Then play with the stuff
that's *working* and start to improve it (or simplify it event) - you
should not move onto any new challenges until you know that code
inside and out and understand what each and every character is there
for. And IMO that requires exercising it iteratively as you make
changes.

And please be sensitive to the challenges of your readers here - you
are asking for help on specific topics and the temptation is to help
you. You've got the best humans on earth (excluding me, work in
progress) reading your challenges and thinking carefully about your
learning process and the issues you're facing - and then taking a
great deal of time and care to respond constructively. If someone
slips up and give you an answer here or there, please forgive them as
they are ony trying to help you.



More information about the erlang-questions mailing list