美文网首页
PL-SML2: Data-Type

PL-SML2: Data-Type

作者: 安之 | 来源:发表于2014-06-14 12:06 被阅读0次

    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

          本文链接:https://www.haomeiwen.com/subject/xxfjtttx.html