lists

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

lists

Sam Overdorf
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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: lists

zxq9-2
On 2019/09/17 8:44, Sam Overdorf 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.

"sam1" is syntactic sugar for [$s, $a, $m, $1].

["sam1", "sam2"] is likewise syntactic sugar for
[[$s, $a, $m, $1], [$s, $a, $m, $1]]

With this in mind, if I want to take the first 's' character from "sam1"
I can do:

[H | _] = "sam1".

If I want to get the first character from the second string in a list of
strings I could do this:

1> [_, [H | _]] = ["sam", "bob"].
["sam","bob"]
2> H.
98
3> $b.
98

The syntax $[character] is a shorthand for the unicode codepoint of a
given character. 98 is the numeric value of the character 'b', so that
is what we see there.

With this in mind, I can construct the string/list "sam" by finding the
codepoints for the characters 's', 'a', and 'm' and putting the numeric
values in a list:

4> $s.
115
5> $a.
97
6> $m.
109
7> [115, 97, 109].
"sam"

Play around with lists inside of lists a bit and you'll find that a list
of strings is just a nested list. There are many convenience functions
for operating over deep lists of strings like this in the unicode and
string modules.

-Craig
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: lists

Richard O'Keefe
In reply to this post by Sam Overdorf
"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 <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: lists

Valentin Micic-6
In reply to this post by Sam Overdorf
On 17 Sep 2019, at 01:44, Sam Overdorf <[hidden email]> 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

I don’t think you could accomplish this without writing some additional code.
A naive example may be:

% ---------
% Normalize
% ---------
get_sam( [[E|L]|_] ) -> get_sam( L, [E] );
get_sam( [[]|T] )    -> get_sam( T );
get_sam( [E|L] )     -> get_sam( L, [E] ); 
get_sam( _ )         -> throw( no_sam ).

% --------------
% Accumulate SAM
% --------------
get_sam( [E|L], Acc ) -> get_sam( L, [E|Acc] );
get_sam( [], Acc )    -> lists:reverse( Acc ).

NOTE: Didn’t test it, but it should achieve what you’ve described.

Kind regards

V/




_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions