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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容