PL-SML2: Data-Type

Data-type:

  • Tuple: each of types, refer to values by position
  • Option: one of types
  • List: three building blocks
  • Records: refer to values by fields or names
Constructor and Deconstrctor:

  • Constructor:
    E.g.
    datatype mytype = TwoInts of int*int
    | Str of String
    | Pizza

  • Deconstructor:
    fun f x =
    case x of
    Pizza => 3
    | TwoInts(i1, i2) => i1 + i2
    | Str s => String.size s

Pattern Matching

case of

Exception

exception MyFirstException
exception MySecondException of int*int
raise MyFirstExcetion
raise (MySecondException(7,9))
e1 handle MyFirstException => e2
e1 handle MySecondException(7,9) => e2
Polymorphic datatypes

   datatype `a option = NONE | SOME `a
   datatype `a mylist = Empty | Cons of `a * `a mylist
   datatype (`a, `b) tree = 
            Node of `a*(`a, `b) tree *(`a, `b) tree
          | Leaf of `b
Syntactic Sugar:

Syntactic: can describe the semantics entirely by the corresponding record syntax
Sugar: make the language sweeter

Tail-recursion

tail-call : if the result of f x is the "immediate result" for the enclosing function body, the f x is a tail-call.
Precise definition: A tail-call is a function call in tail position.

你可能感兴趣的:(PL-SML2: Data-Type)