Presentation is loading. Please wait.

Presentation is loading. Please wait.

An aggregation mechanism

Similar presentations


Presentation on theme: "An aggregation mechanism"— Presentation transcript:

1 An aggregation mechanism
Lists An aggregation mechanism

2 Introduction Like a tuple, a list is a language construct which can be used to aggregate values. Syntactically, a list looks similar to a tuple. The only difference is that in a list, the open/close symbols are square brackets (while in a tuple the open/close symbols are parentheses). Tuple List () [] (1) [1] (1,2) [1,2]

3 [expression1, expression2, … , expressionn]
Syntax In general, a list is a sequence of comma-separated expressions enclosed in square brackets. [expression1, expression2, … , expressionn]

4 Homogeneous Aggregation
Though they are syntactically similar, lists and tuples are very different semantically. A tuple can be used to group values of any type, whereas a list can only group values belonging to a single type. For example, in SML it is perfectly legal to write (1,”hello”). However, writing [1,”hello”] results in a compile-time type error since the first element of the list is of type int while the second element of the list is of type string. To highlight this distinction, we say that tuples are a heterogeneous aggregation mechanism, and lists are a homogeneous aggregation mechanism.

5 Lists vs Tuples Because of their homogeneous nature, lists can be manipulated in important ways that tuples cannot. For example, it is possible to concatenate two lists to form a larger list. Elements can also be removed from lists to produce smaller lists. The central idea here is that lists can get larger and smaller dynamically (i.e., during program execution). In contrast, the size of a tuple is static and remains fixed during program execution. This flexibility makes the list a workhorse of SML (as well as other functional programming languages).

6 The Type of a List In order for lists to change their size and remain well-typed, the number of elements in a list cannot be part of its type. For example, the lists [1,2,3] and [4,5] both have the type int list. In contrast, the tuple (1,2,3) is of type int*int*int while the tuple (4,5) is of type int*int.

7 Examples List Comment [1,2,3,4] A list of integers.
[ [1], [2,3,1], [5,5] ] A list of integer lists. [ [“hello”, “world”], [“I”, “am”, “Sam”] ] A list of string lists. [ (0,1), (4,3), (6,2) ] A list of int x int tuples. [ BLUE, RED, GREEN, YELLOW ] A list of Bricklayer LEGO bricks. [ (BLUE, (0,0)), (RED, (1,0)), (GREEN, (2,0)) ] A list of tuples, where each tuple denotes a brick and a 2D coordinate.

8 List Operators and Functions

9 Construction and Concatenation
SML provides two primitive operators for manipulating lists. The first operator is the construction operator and is generally referred to as the cons operator. The second operator is the concatenation operator.

10 The Cons Operator In SML, the symbol :: is used to denote the cons operator. The cons operator is a binary infix operator. Given an element x and a list of elements xs the expression  x :: xs  denotes a list expression. Let x = 1 and xs = [2,3,4]. The evaluation of the expression x::xs will produce the list value [1,2,3,4] as its result.

11 The Concatenation Operator
In SML, the is used to denote the concatenation operator. The concatenation operator is also a binary infix operator. The concatenation operator may only be applied to two lists of the same type. For example, an int list and a string list cannot be concatenated because they are different types of lists.

12 The Functions hd and tl SML provides two basic functions for decomposing a list. The first function is called head and is denoted hd. The second function is called tail and is denoted tl. Both functions are unary functions, meaning they are only applied to a single argument. When applied to a list, the function hd returns the first element of the list (e.g., hd [1,2,3] = 1). When applied to a list, the function tl returns a list constructed from all but the first element of the input list (e.g., tl [1,2,3] = [2,3]).

13 Examples of List Expressions
Evaluation Result 1 :: [] [1] [] 1 :: [2,3,4] [1,2,3,4] [2,3,4] [1] :: [[2,3],[4]] [[1],[2,3],[4]] [3,4] hd [1,2,3] 1 tl [1,2,3] [2,3]

14 Evaluating List Expressions
Evaluate all hd and tl function applications (i.e., function calls) in a left-to-right order. Evaluate all :: operators in a right-to-left order.

15 Examples List Expression Evaluation Sequence tl [1,2,3] @ [3,4]
 [3,4]  [2,3,3,4] hd [1,2,3] :: [3,4]  1 :: [3,4]  [1,3,4] tl [3,4]  [4]  [1,2,3,4] 4 :: [5,6]  [4,5,6]  [1,2,3,4,5,6] 4 :: [7]  (4 :: [7])))


Download ppt "An aggregation mechanism"

Similar presentations


Ads by Google