[erlang-questions] Q: Differenciating between List and List of Lists

Richard O'Keefe ok@REDACTED
Thu Oct 16 00:34:04 CEST 2008

On 15 Oct 2008, at 6:46 pm, jm wrote:

> What is the best way to tell the difference between a list and a  
> list of
> lists (possible in a guard)?

It's impossible: an empty list and an empty list of lists
are the same thing.

You can tell the difference between a non-empty list of
non-empty lists and any other list thus:

case Thingy
   of [[_|_]|_]         -> non-empty list of lists
    ; X when is_list(X) -> any other list
    ; _                 -> not a list at all

If you are specifically interested in distinguishing
between strings and lists of strings,

if is_integer(hd(Thingy)) -> non-empty string
  ; is_list(   hd(Thingy)) -> non-empty list of strings
  ; Thingy =:= []          -> empty so could be either
  ; true                   -> not a list at all

There are more variations on both of these than I have time
to write or you to read.

More information about the erlang-questions mailing list