[erlang-questions] lists

Valentin Micic v@REDACTED
Tue Sep 17 13:39:33 CEST 2019


> On 17 Sep 2019, at 01: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

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/



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190917/5676ddf3/attachment.htm>


More information about the erlang-questions mailing list