[erlang-questions] Help for a newbie...

Bruno Girin brunogirin@REDACTED
Sat May 11 16:22:03 CEST 2013


Another question from a newbie based on your code above. In this line:

[Time,_|Dishes] = string:tokens(User_Entry, ","),

The way I understand it, Time will point to the first token, the second
will be dropped (as it maps to _) and Dishes will be the rest of the list.
Am I correct?

If yes, did you include _ to reflect the original code where
lists:nthtail(AllTokens, 2) would have resulted in Dishes starting at the
3rd token effectively ignoring the 2nd token? I'm not sure that's what was
intended based on the description. Also, would it be sensible to add space
as a separator to avoid having them included in tokens?

In which case, that line may be a bit closer to what Boris intended.

[Time|Dishes] = string:tokens(User_Entry, ", "),

Please correct me if I misunderstood something, I'm learning Erlang too :-)

Cheers,

Bruno



On 9 May 2013 02:10, Richard A. O'Keefe <ok@REDACTED> wrote:

> I wonder if the original poster would have been so puzzled had
> the error message been
>
> "A function cannot begin with 'case'."
>
> which would have hinted that the compiler thought this was
> the beginning of a (new) function.
>
> The last parser I wrote, I switched from Yacc to plain old
> recursive descent and found it much easier to generate
> error messages that I found helpful.
>
> The original poster should also note that
> itIsNotConsideredGoodErlangStyleToUseWordsRunTogetherLikeThis.
> Multi-word identifiers in Erlang are normally separated by
> spaces unless they have to be compatible with something else.
> Itisalsonotgoodtorunwordstogetherwithnoindication.
>
> There is a problem with the calls to nthtail/2 (just because
> it's in the Erlang standard library doesn't mean it's a good
> name):  the arguments are in the wrong order.  In any case,
> you don't need it.
>
> -module(practicum).
> -export([place_order/1]).
>
> place_order(User_Entry) ->
>     [Time,_|Dishes] = string:tokens(User_Entry, ","),
>     case Time
>       of "morning" -> process_morning(Dishes)
>        ; "night"   -> process_evening(Dishes)
>        ; _         -> io:fwrite("error\n")
>     end.
>
> process_morning(Dishes) ->
>     Dishes.
>
> process_evening(Dishes) ->
>     Dishes.
>
> You might prefer to eliminate the '_ -> io:fwrite(...)' case;
> if there's no match the 'case' will crash, and that's just what
> we want.  Unless this is an assignment, in which case do what
> the assignment says.
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



-- 
Bruno
Visit my weblog: http://brunogirin.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130511/b69644f9/attachment.htm>


More information about the erlang-questions mailing list