chapter 2 ml a functional programming language
play

Chapter 2 ML, a Functional Programming Language (Version of 24 - PDF document

Ch.2: ML, a Functional Programming Language Plan Chapter 2 ML, a Functional Programming Language (Version of 24 September 2004) 1. Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 2. Value declarations


  1. Ch.2: ML, a Functional Programming Language Plan Chapter 2 ML, a Functional Programming Language (Version of 24 September 2004) 1. Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 2. Value declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.13 3. Function declarations . . . . . . . . . . . . . . . . . . . . . . . . . . 2.16 4. Type inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.18 5. Anonymous functions . . . . . . . . . . . . . . . . . . . . . . . . . . 2.20 6. Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.22 7. Tuples and records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.24 8. Functions with several arguments/results . . . . . . . 2.26 9. Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.28 10. Pattern matching and case analysis . . . . . . . . . . . 2.32 11. Local declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.36 12. New operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.39 13. Recursive functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.40 14. Side effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.41 15. Exception declarations . . . . . . . . . . . . . . . . . . . . . . . . 2.42 16. Functional languages vs. imperative languages 2.46 2.1 � P. Flener/IT Dept/Uppsala Univ. c FP

  2. Ch.2: ML, a Functional Programming Language 2.1. Expressions 2.1. Expressions Interacting with ML 32 + 15 ; - val it = 47 : int 3.12 ∗ 4.3 ; - val it = 13.416 : real not true ; - val it = false : bool "The Good, the Bad," � " and the Ugly" ; - val it = "The Good, the Bad, and the Ugly" : string size ("Esra") ( + - size ("Pierre") ) div 2 ; = val it = 5 : int • ML has an interpreter • ML is a typed language 2.2 � P. Flener/IT Dept/Uppsala Univ. c FP

  3. Ch.2: ML, a Functional Programming Language 2.1. Expressions Basic types • unit : only one possible value: () • int : integers • real : real numbers • bool : truth values (or: Booleans) true and false • char : characters • string : character sequences Operators • We use operator and function as synonyms • We use argument , parameter , and operand as synonyms Operator types 2 + 3.5 ; - ! 2 + 3.5 ; ! ˆˆˆ ! Type clash: expression of type real ! cannot have type int The operators on the basic types are thus typed : no mixing, no implicit conversions! For convenience, the arithmetic operators are overloaded : the same symbol is used for different operations, but they have different realisations; for instance: + : int × int → int + : real × real → real 2.3 � P. Flener/IT Dept/Uppsala Univ. c FP

  4. Ch.2: ML, a Functional Programming Language 2.1. Expressions Integers Syntax • As usual, except the unary operator − is represented by � • Example: �123 Basic operators on the integers op : type form precedence + : int × int → int infix 6 − : int × int → int infix 6 ∗ : int × int → int infix 7 div : int × int → int infix 7 mod : int × int → int infix 7 int × int → bool ∗ infix = : 4 int × int → bool ∗ infix <> : 4 < : int × int → bool infix 4 < = : int × int → bool infix 4 > : int × int → bool infix 4 > = : int × int → bool infix 4 ˜ : int → int prefix abs : int → int prefix ( ∗ the exact type will be defined later) • The infix operators associate to the left • Their operands are always all evaluated 2.4 � P. Flener/IT Dept/Uppsala Univ. c FP

  5. Ch.2: ML, a Functional Programming Language 2.1. Expressions Real numbers Syntax • As usual, except the unary operator − is represented by � • Examples: 234.2 , �12.34 , �34E2 , 4.57E�3 Basic operators on the reals op : type form precedence + : real × real → real infix 6 − : real × real → real infix 6 ∗ : real × real → real infix 7 / : real × real → real infix 7 real × real → bool ∗ infix = : 4 real × real → bool ∗ infix <> : 4 < : real × real → bool infix 4 < = : real × real → bool infix 4 > : real × real → bool infix 4 > = : real × real → bool infix 4 ˜ : real → real prefix abs : real → real prefix Math.sqrt : real → real prefix Math.ln : real → real prefix ( ∗ the exact type will be defined later) • The infix operators associate to the left • Their operands are always all evaluated 2.5 � P. Flener/IT Dept/Uppsala Univ. c FP

  6. Ch.2: ML, a Functional Programming Language 2.1. Expressions Characters and strings Syntax • A character value is written as the symbol # immediately followed by the character enclosed in double-quotes " • A string is a character sequence enclosed in double-quotes " • Control characters can be included: end-of-line: \ n double-quote: \ " backslash: \\ Basic operators on the characters and strings Let ‘strchar × strchar’ be ‘char × char’ or ‘string × string’ op : type form precedence strchar × strchar → bool ∗ infix = : 4 strchar × strchar → bool ∗ infix <> : 4 < : strchar × strchar → bool infix 4 < = : strchar × strchar → bool infix 4 > : strchar × strchar → bool infix 4 > = : strchar × strchar → bool infix 4 ˆ : string × string → string infix 6 size : string → int prefix ( ∗ the exact type will be defined later) Use of the lexicographic order , according to the ASCII code • The infix operators associate to the left • Their operands are always all evaluated 2.6 � P. Flener/IT Dept/Uppsala Univ. c FP

  7. Ch.2: ML, a Functional Programming Language 2.1. Expressions Booleans Syntax • Truth values true and false • Attention: True is not a value of type bool : ML distinguishes uppercase and lowercase characters! Basic operators on the Booleans op : type form precedence andalso : bool × bool → bool infix 3 orelse : bool × bool → bool infix 2 not : bool → bool prefix bool × bool → bool ∗ infix = : 4 bool × bool → bool ∗ infix <> : 4 ( ∗ the exact type will be defined later) Truth table A B A andalso B A orelse B true true true true true false false true false true false true false false false false 2.7 � P. Flener/IT Dept/Uppsala Univ. c FP

  8. Ch.2: ML, a Functional Programming Language 2.1. Expressions • The infix operators associate to the left • The second operand of andalso & orelse is not always evaluated: lazy logical and & or Example: orelse Math.ln (12.4) ∗ 3.4 > 12.0 ) ; ( 34 < 649 ) ( - val it = true : bool The second operand, namely ( Math.ln (12.4) ∗ 3.4 > 12.0 ) , is not evaluated because the first operand evaluates to true Another example: orelse ( 34 < 649 ) ( 0.0 / 0.0 > 999.9 ) ; - val it = true : bool The second operand ( 0.0 / 0.0 > 999.9 ) is not evaluated, even though by itself it would lead to an error: ( 0.0 / 0.0 > 999.9 ) ; - ! Uncaught exception: Div 2.8 � P. Flener/IT Dept/Uppsala Univ. c FP

  9. Ch.2: ML, a Functional Programming Language 2.1. Expressions Type conversions op : type real : int → real ceil : real → int �oor : real → int round : real → int trunc : real → int real (2) + 3.5 ; - val it = 5.5 : real ceil (23.65) ; - val it = 24 : int ceil (�23.65) ; - val it = ˜23 : int �oor (23.65) ; - val it = 23 : int �oor (�23.65) ; - val it = ˜24 : int round (23.65) ; - val it = 24 : int round (23.5) ; - val it = 24 : int round (22.5) ; - val it = 22 : int 2.9 � P. Flener/IT Dept/Uppsala Univ. c FP

  10. Ch.2: ML, a Functional Programming Language 2.1. Expressions trunc (23.65) ; - val it = 23 : int trunc (�23.65) ; - val it = ˜23 : int op : type chr : int → char ord : char → int str : char → string chr(97) ; - val it = #"a" : char ord(#"a") ; - val it = 97 : int str(#"a") ; - val it = "a" : string Conversions are done according to the ASCII code 2.10 � P. Flener/IT Dept/Uppsala Univ. c FP

  11. Ch.2: ML, a Functional Programming Language 2.1. Expressions Evaluation of expressions Reduction 3 + 4 ∗ 2 < 5 ∗ 2 ❀ 3 + 8 < 5 ∗ 2 ❀ 11 < 5 ∗ 2 ❀ 11 < 10 ❀ false • Note the precedence of the operators • Reduction to a normal form (a form that cannot be further reduced) • This normal form is the result of the evaluation • The type of the result is inferred from those of the operators Principles Reduction (evaluation) of the expression E 1 op E 2 1.Reduction of the expression E 1 : E 1 ❀ . . . ❀ N 1 2.Reduction of the expression E 2 : E 2 ❀ . . . ❀ N 2 unless op is lazy and N 1 is such that E 2 need not be reduced 3.Application of the operator op to N 1 and N 2 Evaluation from left to right: first E 1 then E 2 (if necessary) 2.11 � P. Flener/IT Dept/Uppsala Univ. c FP

Recommend


More recommend