Changeset 423c0cd for doc


Ignore:
Timestamp:
Mar 12, 2024, 3:14:47 PM (4 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
8bdc9705, a3525c4
Parents:
9a32903
Message:

more updates on OCaml

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/relatedwork.tex

    r9a32903 r423c0cd  
    25022502\end{ocaml}
    25032503
    2504 A variant type can have a recursively definition.
     2504A variant type can have a recursive definition.
    25052505\begin{ocaml}
    25062506type stringList = Empty | Pair of string * stringList
     
    25092509A recursive function is often used to pattern match against a recursive variant type.
    25102510\begin{ocaml}
    2511 let rec len_of_string_list(l: stringList): int =
    2512         match l with
     2511let rec len_of_string_list( list : stringList ): int =
     2512        match list with
    25132513                Empty -> 0 |
    2514                 Pair(_ , r) -> 1 + len_of_string_list r
     2514                Pair( _ , r ) -> 1 + len_of_string_list r
    25152515\end{ocaml}
    2516 
    2517 Note, matching is logically a dynamic type-check.
    2518 Hence, a tagged variant has no notion of enumerabilty, and therefore is not an enumeration, except for the simple pure (untyped) case.
     2516Here, the head of the recursive type is removed and the remainder is processed until the type is empty.
     2517Each recursion is counted to obtain the number of elements in the type.
     2518
     2519Note, the compiler statically guarantees that only the correct kind of type can be used in the \lstinline[language=ML]{match} statement.
     2520However, the tag is dynamically set on binding (and possible reset on assignment), so the \lstinline[language=ML]{match} statement is effectively doing RTTI to select the matching case clause.
     2521Hence, a tagged variant has no notion of enumerabilty, and therefore is not a real enumeration, except for the simple pure (untyped) case.
Note: See TracChangeset for help on using the changeset viewer.