B.6 Lists

The following are the some important points about lists in ML.

  • Unlike in Scheme, lists in ML are homogeneous, meaning all elements of the list must be of the same type. For instance, the list [1,2,3] in ML is homogeneous, while the list (1 "apple") in Scheme is heterogeneous.

  • In a type-safe language like ML the values in a tuple (Section B.7) generally have different types, but the number of elements in the tuple must be fixed. Conversely, the values of a list must all have the same type, but the number of elements in the list is not fixed.

  • The semantics of the lexemes nil and [] are the empty list.

  • The cons operator, which accepts an element (the head) and a list (the tail), is :: (e.g., 1::2::[3]) and associates right-to-left.

  • The expression x::xs represents a list of at least one element.

  • The expression xs is pronounced exes.

  • The expression x::nil represents a list of exactly one element and is the same as [x].

  • The expression x::y::xs represents a list of at least two elements.

  • The expression x::y::nil represents a list of exactly two elements.

  • The built-in functions hd (for head) and tl (for tail) are the ML analogs of the Scheme functions car and cdr, respectively.

  • The built-in function length returns the number of elements in its only list argument.

  • The append operator (@) accepts two lists and appends them to each other. For example, [1, 2] @ [3, 4, 5] returns [1, 2, 3, 4, 5]. The append operator in ML is also inefficient, just as it is in Scheme.

Examples:

A set of 12 code lines in M L that consists of a list.
Description
Continuation of the code in M L that consists of a list with 12 code lines.
Description
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.37.154