3 1 lists
play

3.1. Lists Chapter 3 Linear Structures: Lists Definition A list is - PDF document

Ch.3: Linear Structures: Lists Ch.3: Linear Structures: Lists Plan 3.1. Lists 3.1. Lists Chapter 3 Linear Structures: Lists Definition A list is an element collection with the following properties: Homogeneity: all elements are of the


  1. Ch.3: Linear Structures: Lists Ch.3: Linear Structures: Lists Plan 3.1. Lists 3.1. Lists Chapter 3 Linear Structures: Lists Definition A list is an element collection with the following properties: • Homogeneity: all elements are of the same type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 1. Lists • Variability: the number of elements is arbitrary 2. Basic operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.4 • Multiplicity: an element may appear several times in a list • Extensionality: all elements must be explicitly given . . . . . . . . . . . . . . . . . . .. . . . . 3.5 3. Constructors and pattern matching • Linearity: the internal structure is linear 4. Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.8 Examples of ML lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.11 5. Simple operations on lists • [18, 12, ˜5, 3+7] is an integer list (this type is denoted int list ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.16 6. Application: polynomials • [2.0, 5.3 / 3.7, Math.sqrt 27.3] is a real-number list ( real list ) • ["Pierre", "Esra"] is a list of strings ( string list ) • [(1,"A"), (2,"B")] is a list of integer-string pairs ( (int ∗ string) list ) • [ [4,5], [8], [12,˜3,0] ] is a list of integer lists ( int list list ) • [even, odd] is a list of int → bool functions ( (int → bool) list ) • [12, 34.5] is not a list Sven-Olof Nystr¨ om/IT Dept/Uppsala University 3.1 Sven-Olof Nystr¨ om/IT Dept/Uppsala University 3.2 FP FP Ch.3: Linear Structures: Lists 3.1. Lists Ch.3: Linear Structures: Lists 3.2. Basic operations 3.2. Basic operations The empty list The empty list is denoted [] or [ ] or nil null [ ] ; - val it = true : bool What is the type of the empty list?! hd [1,2,3] ; - val it = 1 : int The empty list must be of the type int list and real list and string list and int list list and (int → bool) list and . . . tl [1,2,3] ; - val it = [2,3] : int list The solution is that [ ] is a polymorphic value : tl [1] ; - it belongs to several types! val it = [] : int list tl [ ] ; - Type expressions ! Uncaught exception: Empty We use type variables to express polymorphic types : 3 :: [8,2] ; - val it = [3,8,2] : int list The type of [ ] is α list 3 :: [ ] ; - where α is a type variable, denoting an arbitrary type val it = [3] : int list [3] :: [8,2] ; In ML, type variables are written ’a ’b . . . - ! [3] :: [8,2] ; ! ˆ [ ] ; - ! Type clash: expression of type int val ’a it = [] : ’a list ! cannot have type int list 3 :: 8 ; - ! 3 :: 8 ; ! ˆ ! Type clash: expression of type int ! cannot have type int list Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP 3.3 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP 3.4

  2. Ch.3: Linear Structures: Lists Ch.3: Linear Structures: Lists 3.3. Constructors and pattern matching 3.3. Constructors and pattern matching 3.3. Constructors and pattern matching Pattern matching Use pattern matching for: The expression [3,8] is syntactic sugar for 3 :: ( 8 :: [ ] ) • Decomposing an object into its parts • Accessing the parts of a constructed object The symbol :: is not an ML function, but a value constructor : val (x::xs) = [3,8,5] ; • Composition of a new object from its parts - val x = 3 : int • Decomposition of an object into its parts val xs = [8,5] : int list Only constructors can be used in patterns val (x::xs) = [3] ; - Another value constructor for lists is [ ] val x = 3 : int val xs = [] : int list One can indifferently use the aggregated form [3,8,5] val (x::xs) = [ ] ; and the constructed form 3 :: 8 :: 5 :: [ ] - ! Uncaught exception: Bind as they represent the same object! 1 :: 2 :: [ ] = [1,2] ; - Example: concatenating two lists (append.sml) val it = true : bool fun append [ ] ys = ys | append (x::xs) ys = x :: (append xs ys) In ML, the symbol :: is thus a value constructor that is: • The two lines of this function declaration are called clauses • binary • A list concatenation function is actually predefined in ML, • infix namely as the (binary, infix, right-associating) operator @ • right -associating: for instance, 3 :: 8 :: [ ] is 3 :: ( 8 :: [ ] ) • of the functional type α ∗ α list → α list • The patterns [ ] and (x::xs) are mutually exclusive • The pattern [x] is equivalent to ( x::[ ] ) Sven-Olof Nystr¨ om/IT Dept/Uppsala University 3.5 Sven-Olof Nystr¨ om/IT Dept/Uppsala University 3.6 FP FP Ch.3: Linear Structures: Lists 3.3. Constructors and pattern matching Ch.3: Linear Structures: Lists 3.4. Polymorphism 3.4. Polymorphism Lists vs. tuples Tuples: example (3, 8.0, 5>8) function hd X TYPE: α list → α • Fixed size PRE: X is not the empty list • Heterogeneous (components of possibly different types) POST: the head of X • Direct access to the components via the # i selectors fun hd [ ] = error "hd: empty list" | hd (x::xs) = x Lists: example [3, 8, 5] hd [1,2] ; - val it = 1 : int • Arbitrary length hd [true,false,true] ; • Homogenous (elements of the same type) - val it = true : bool • Access to the parts via pattern matching with hd and tl that is: sequential access to the elements function first (a,b) TYPE: α ∗ β → α Constructed form vs. aggregated form PRE: (none) POST: the first component of the pair (a,b) The aggregated form of lists is mostly used for: fun first (a,b) = a • Arguments first ([4,5], true) ; • Results (when displayed by ML) - val it = [4,5] : int list [3,4,5] @ [6,7] ; - first (hd, 3.5) ; val it = [3,4,5,6,7] : int list - val it = fn : ’a list -> ’a The constructed form is mostly used in function declarations: The functions hd and first can be used with arguments of varying • Decomposition of a list by pattern matching types, without changing their names or declarations: polymorphism • Composition of a list Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP 3.7 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP 3.8

  3. Ch.3: Linear Structures: Lists Ch.3: Linear Structures: Lists 3.4. Polymorphism 3.4. Polymorphism The type of our error function The polymorphism of member must be restricted to the types for which the equality test is computable, It must be possible to use error in any situation: that is to the types of objects without functions the type of its result is thus some type variable, say α function error msg These equality types are denoted by variables of the form α = β = TYPE: string → α . . . , or ´´a ´´b . . . in ML PRE, POST: (none) SIDE-EFFECT: displays msg to the screen and halts the execution function member v X TYPE: α = → α = list → bool PRE: (none) The type of = ( equality ) POST: true if v is an element of X false otherwise Example: membership of an object in a list (member.sml) function x = y function member v X TYPE: α = ∗ α = → bool TYPE (tentatively): α → α list → bool PRE: (none) PRE: (none) POST: true if x = y POST: true if v is an element of X false otherwise false otherwise function x <> y fun member v [ ] = false TYPE: α = ∗ α = → bool | member v (x::xs) = (v=x) orelse member v xs PRE: (none) The member function is polymorphic: POST: true if x � = y false otherwise • It can be used with objects where α is the type int, real, bool, (int ∗ bool), int list , . . . • It cannot be used with objects Example: where α is the type (int → int), (int → bool), (real → real) , . . . fun member . . . ; because the equality test between two functions - val ’’a member = fn : ’’a -> ’’a list -> bool is not computable! Sven-Olof Nystr¨ om/IT Dept/Uppsala University 3.9 Sven-Olof Nystr¨ om/IT Dept/Uppsala University 3.10 FP FP Ch.3: Linear Structures: Lists 3.5. Simple operations on lists Ch.3: Linear Structures: Lists 3.5. Simple operations on lists 3.5. Simple operations on lists General schema For most of the simple operations on lists, the form of the constructed ML program will be: Reversal of a list (reverse.sml) fun f [ ] . . . = . . . | f (x::xs) . . . = . . . ( f xs) . . . Specification function reverse X Length of a list (length.sml) TYPE: α list → α list PRE: (none) function length X POST: the reverse list of X TYPE: α list → int PRE: (none) Construction with the length of X as variant POST: the number of elements of X fun length [ ] = 0 Base case : X is [ ] : return [ ] | length (x::xs) = 1 + length xs The length function is actually predefined in ML General case : X is of the form (x::xs) : return reverse xs @ [x] ML program Product of the elements of a list (prod.sml) fun reverse [ ] = [ ] | reverse (x::xs) = reverse xs @ [x] function prod X TYPE: int list → int PRE: (none) The list reversal function is actually predefined, as rev POST: the product of the elements of X fun prod [ ] = 1 | prod (x::xs) = x ∗ prod xs Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP 3.11 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP 3.12

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend