[erlang-questions] Baffling lists

Richard A. O'Keefe ok@REDACTED
Mon Sep 22 10:50:19 CEST 2014


On 22/09/2014, at 3:07 AM, Stu Bailey wrote:

> Thanks.  I actually wanted to reverse the list of lists lists:reverse(L4).  I did not know about "irregular lists".  Now I do. 

In the Prolog world they're usually called "improper lists".
In fact, that's what they are called in Erlang too.
From http://www.erlang.org/doc/reference_manual/typespec.html

List :: list(Type)
            %% Proper list ([]-terminated)
      | maybe_improper_list(Type1, Type2)
            %% Type1=contents, Type2=termination
      | nonempty_improper_list(Type1, Type2)
            %% Type1 and Type2 as above
      | nonempty_list(Type)
            %% Proper non-empty list

list()	               = [any()]
maybe_improper_list()  = maybe_improper_list(any(), any())
nonempty_list()        = nonempty_list(any())

"What's going on in general" is that "list" is not a primitive
data structure in Erlang (or Prolog, or Lisp, where it all
started).  [] is a primitive data structure.  A pair [H|T] is
a primitive data structure.  But a list is just one way to
build things out of pairs.  See iolist, for example:

iolist() =
    maybe_improper_list(byte() | binary() | iolist(),
                        binary() | [])





More information about the erlang-questions mailing list