Changeset 423c0cd
- Timestamp:
- Mar 12, 2024, 3:14:47 PM (9 months ago)
- Branches:
- master
- Children:
- 8bdc9705, a3525c4
- Parents:
- 9a32903
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/relatedwork.tex
r9a32903 r423c0cd 2502 2502 \end{ocaml} 2503 2503 2504 A variant type can have a recursive lydefinition.2504 A variant type can have a recursive definition. 2505 2505 \begin{ocaml} 2506 2506 type stringList = Empty | Pair of string * stringList … … 2509 2509 A recursive function is often used to pattern match against a recursive variant type. 2510 2510 \begin{ocaml} 2511 let rec len_of_string_list( l: stringList): int =2512 match l with2511 let rec len_of_string_list( list : stringList ): int = 2512 match list with 2513 2513 Empty -> 0 | 2514 Pair( _ , r) -> 1 + len_of_string_list r2514 Pair( _ , r ) -> 1 + len_of_string_list r 2515 2515 \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. 2516 Here, the head of the recursive type is removed and the remainder is processed until the type is empty. 2517 Each recursion is counted to obtain the number of elements in the type. 2518 2519 Note, the compiler statically guarantees that only the correct kind of type can be used in the \lstinline[language=ML]{match} statement. 2520 However, 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. 2521 Hence, 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.