[erlang-questions] Q: Differenciating between List and List of Lists
Roberto Aloi
roberto@REDACTED
Wed Oct 15 10:57:29 CEST 2008
> What is the best way to tell the difference between a list and a list of
> lists (possible in a guard)? What I'm trying to differentiate between is
> a string, eg, "astring" and a list of strings, eg ["a", "list", "of",
> "strings"].
>
I think the best way should be to organize your application so that you
have a list containing one element only in the first case.
I mean: ["astring"] instead of simply "astring".
The main problem is that strings are not a data type in Erlang, but they
are just lists of integers.
If the above solution is not applicable for you, just check for the type
of the contained elements.
Something like:
1> List = "aList".
"aList"
2> ListOfLists = ["first", "second"].
["first","second"]
3> is_list(lists:nth(1,List)).
false
4> is_list(lists:nth(1,ListOfLists)).
true
5> is_integer(lists:nth(1,ListOfLists)).
false
6> is_integer(lists:nth(1,List)).
true
Just remember to check for empty lists!!!
Roberto Aloi
More information about the erlang-questions
mailing list