Functional Programming: what is an “improper list”?

前端 未结 9 1035
遥遥无期
遥遥无期 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
    

提交回复
热议问题