2 1 expressions
play

2.1. Expressions SML, a Functional Programming Language Interacting - PDF document

Ch.2: SML, a Functional Programming Language Plan Ch.2: SML, a Functional Programming Language 2.1. Expressions Chapter 2 2.1. Expressions SML, a Functional Programming Language Interacting with ML . . . . . . . . . . . . . . . . . . . . . .


  1. Ch.2: SML, a Functional Programming Language Plan Ch.2: SML, a Functional Programming Language 2.1. Expressions Chapter 2 2.1. Expressions SML, a Functional Programming Language Interacting with ML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 1. Expressions 2. Value declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . 2.13 32 + 15 ; - val it = 47 : int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.16 3. Function declarations 3.12 ∗ 4.3 ; - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.18 4. Type inference val it = 13.416 : real . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.20 5. Anonymous functions not true ; - . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.22 6. Specifications val it = false : bool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.24 7. Tuples and records "The Good, the Bad," ˆ " and the Ugly" ; - . . . . . . . . . . . . . . . . . . 2.26 8. Functions with several arguments/results val it = "The Good, the Bad, and the Ugly" : string ( size ("Esra") + . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.28 - 9. Currying size ("Pierre") ) div 2 ; = . . . . . . . . . . . . . . . . . . . . . . 2.31 10. Pattern matching and case analysis val it = 5 : int 11. Local declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.35 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.37 12. New operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.38 13. Recursive functions 14. Side effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.39 . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . 2.41 15. Exception declarations . . . . . . .. . . . 2.42 16. Functional languages vs. imperative languages Sven-Olof Nystr¨ om/IT Dept/Uppsala University 2.1 Sven-Olof Nystr¨ om/IT Dept/Uppsala University 2.2 FP FP Ch.2: SML, a Functional Programming Language 2.1. Expressions Ch.2: SML, a Functional Programming Language 2.1. Expressions Basic types Integers • unit : only one possible value: () Syntax • int : integers • real : real numbers • As usual, except the unary operator − is represented by ˜ • bool : truth values (or: Booleans) true and false • Example: ˜123 • char : characters • string : character sequences Basic operators on the integers : op type form precedence Operators + : int × int → int infix 6 • We use operator and function as synonyms − : int × int → int infix 6 ∗ : int × int → int infix 7 • We use argument , parameter , and operand as synonyms div : int × int → int infix 7 mod : int × int → int infix 7 Operator types int × int → bool ∗ infix = : 4 int × int → bool ∗ infix <> : 4 2 + 3.5 ; - < : int × int → bool infix 4 ! 2 + 3.5 ; < = : int × int → bool infix 4 ! ˆˆˆ > : int × int → bool infix 4 ! Type clash: expression of type real > = : int × int → bool infix 4 ! cannot have type int ˜ : int → int prefix The operators on the basic types are thus typed : abs int → int : prefix ( ∗ the exact type will be defined later) no mixing, no implicit conversions! For convenience, the arithmetic operators are overloaded : the same symbol is used for different operations, • The infix operators associate to the left but they have different realisations; for instance: • Their operands are always all evaluated + : int × int → int + : real × real → real Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP 2.3 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP 2.4

  2. Ch.2: SML, a Functional Programming Language 2.1. Expressions Ch.2: SML, a Functional Programming Language 2.1. Expressions Real numbers Characters and strings Syntax Syntax • As usual, except the unary operator − is represented by ˜ • A character value is written as the symbol # immediately followed by the character enclosed in double-quotes " • Examples: 234.2 , ˜12.34 , ˜34E2 , 4.57E˜3 • A string is a character sequence enclosed in double-quotes " Basic operators on the reals • Control characters can be included: end-of-line: \ n double-quote: \ " backslash: \\ op : type form precedence + : real × real → real infix 6 Basic operators on the characters and strings − : real × real → real infix 6 ∗ : real × real → real infix 7 Let ‘strchar × strchar’ be ‘char × char’ or ‘string × string’ / : real × real → real infix 7 real × real → bool ∗ infix = : 4 op : type form precedence real × real → bool ∗ infix <> : 4 strchar × strchar → bool ∗ infix = : 4 < : real × real → bool infix 4 strchar × strchar → bool ∗ infix <> : 4 < = : real × real → bool infix 4 < : strchar × strchar → bool infix 4 > : real × real → bool infix 4 < = : strchar × strchar → bool infix 4 > = : real × real → bool infix 4 > : strchar × strchar → bool infix 4 ˜ : real → real prefix > = : strchar × strchar → bool infix 4 abs : real → real prefix ˆ : string × string → string infix 6 Math.sqrt : real → real prefix size : string → int prefix Math.ln : real → real prefix ( ∗ the exact type will be defined later) ( ∗ 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 • The infix operators associate to the left • Their operands are always all evaluated Sven-Olof Nystr¨ om/IT Dept/Uppsala University 2.5 Sven-Olof Nystr¨ om/IT Dept/Uppsala University 2.6 FP FP Ch.2: SML, a Functional Programming Language 2.1. Expressions Ch.2: SML, a Functional Programming Language 2.1. Expressions • The infix operators associate to the left Booleans • The second operand of andalso & orelse is not always evaluated: lazy logical and & or Syntax Example: • Truth values true and false ( 34 < 649 ) orelse ( Math.ln (12.4) ∗ 3.4 > 12.0 ) ; - • Attention: True is not a value of type bool : val it = true : bool ML distinguishes uppercase and lowercase characters! The second operand, namely ( Math.ln (12.4) ∗ 3.4 > 12.0 ) , is not evaluated because the first operand evaluates to true Basic operators on the Booleans op : type form precedence Another example: andalso : bool × bool → bool infix 3 ( 34 < 649 ) orelse ( 0.0 / 0.0 > 999.9 ) ; - orelse : bool × bool → bool infix 2 val it = true : bool not : bool → bool prefix bool × bool → bool ∗ infix The second operand ( 0.0 / 0.0 > 999.9 ) is not evaluated, = : 4 bool × bool → bool ∗ infix even though by itself it would lead to an error: <> : 4 ( ∗ the exact type will be defined later) - ( 0.0 / 0.0 > 999.9 ) ; ! Uncaught exception: Div Note: = and <> are defined for many (but not all) SML types. 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 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP 2.7 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP 2.8

Recommend


More recommend