[erlang-questions] Help for a newbie...
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 :-)
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
> Multi-word identifiers in Erlang are normally separated by
> spaces unless they have to be compatible with something else.
> 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.
> 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")
> process_morning(Dishes) ->
> process_evening(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
Visit my weblog: http://brunogirin.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions