[erlang-questions] lists
Richard O'Keefe
raoknz@REDACTED
Tue Sep 17 08:14:15 CEST 2019
"sam1" is a list of length 4 whose first element is the integer $s.
["sam1","sam2"] is a list 2 whose first element is the list "sam1".
I'm guessing that you're heading towards a problem that I discussed
in my Prolog book: "How do I design a data structure in this language?"
The basic rule is
- what are the different situations I need to represent?
- for each of these different situations, what additional
information is there?
- decide how to represent that context-dependent information
- in Prolog, give each situation its own function symbol;
in Erlang, consider the {Case_Label,Case_Dependent,Info...}
approach. Whatever you do, make each case OBVIOUSLY different
by a trivial pattern match (or a similarly trivial guard test
in Erlang).
- if some cases have one chunk of info and that is a sequence,
you might use a bare list but ONLY for one.
With a well-designed Erlang data structure, there won't be any
point in asking the question 'how do I tell the difference
between "sam1" and ["sam1","sam2"]' because you will have made
sure it is never the case that both are live possibilities.
For example, if you want to model JSON, you might choose
true | false | null -- the corresponding Erlang atom
Number -- the same number
String -- {string,String}
[T1,....,Tn] -- {array,[T'1,....,T'n]}
{K1:V1,...,Kn:Vn} -- [{K1,V1},...,{Kn,Vn}]
Of course in current Erlang you might use a 'map' for the last.
The point is that {string,"..."} and {array,[...]} are trivially
easy to distinguish.
You might want to give some consideration to using
<<"sam1">> and
[<<"sam1">>,<<"sam2">>]
instead.
Perhaps you could go into more detail about what you want to achieve.
On Tue, 17 Sep 2019 at 15:44, Sam Overdorf <soverdor@REDACTED> wrote:
> How do I tell the difference between:
> "sam1"
> ["sam1","sam2"]
> They are both lists but behave differently when I apply
> [H|T] = List.
> H = "s"
> H = "sam1".
> The second one is what I want and then stop breaking them down.
>
> Weird problem...
>
> Thanks,
> Sam
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190917/89c238a1/attachment.htm>
More information about the erlang-questions
mailing list