Functional Programming: what is an “improper list”?

前端 未结 9 936
遥遥无期
遥遥无期 2020-11-27 04:38

Could somebody explain what an \"improper list\" is?

Note: Thanks to all ! All you guys rock!

相关标签:
9条回答
  • 2020-11-27 05:06

    A list is made up of cells, each cell consisting of two pointers. First one pointing to the data element, second one to the next cell, or nil at the end of the list.

    If the second one does not point to a cell (or nil), the list is improper. Functional languages will most probably allow you to construct cells, so you should be able to generate improper lists.

    In Erlang (and probably in other FP languages as well) you can save some memory by storing your 2-tuples as improper lists:

    2> erts_debug:flat_size({1,2}).
    3
    3> erts_debug:flat_size([1|2]).
    2
    
    0 讨论(0)
  • 2020-11-27 05:08

    I think @Vijay's answer is the best one so far and I just intend to Erlangify it.

    Pairs (cons cells) in Erlang are written as [Head|Tail] and nil is written as []. There is no restriction as to what the head and tail are but if you use the tail to chain more cons cells you get a list. If the final tail is [] then you get a proper list. There is special syntactic support for lists in that the proper list

    [1|[2|[3|[]]]]
    

    is written as

    [1,2,3]
    

    and the improper list

    [1|[2|[3|4]]]
    

    is written as

    [1,2,3|4]
    

    so you can see the difference. Matching against proper/improper lists is correspondingly easy. So a length function len for proper lists:

    len([_|T]) -> 1 + len(T);
    len([]) -> 0.
    

    where we explicitly match for the terminating []. If given an improper list this will generate an error. While the function last_tail which returns the last tail of a list can handle improper lists as well:

    last_tail([_|T]) -> last_tail(T);
    last_tail(Tail) -> Tail.                 %Will match any tail
    

    Note that building a list, or matching against it, as you normally do with [Head|Tail] does not check if the tail is list so there is no problem in handling improper lists. There is seldom a need for improper lists, though you can do cool things with them.

    0 讨论(0)
  • 2020-11-27 05:09

    I think possibly it refers to a "dotted pair" in LISP, e.g. a list whose final cons cell has an atom, rather than a reference to another cons cell or NIL, in the cdr.

    EDIT

    Wikipedia suggests that a circular list also counts as improper. See

    http://en.wikipedia.org/wiki/Lisp_(programming_language)

    and search for 'improper' and check the footnotes.

    0 讨论(0)
提交回复
热议问题