Changeset 9398177
- Timestamp:
- Mar 11, 2024, 5:09:25 AM (9 months ago)
- Branches:
- master
- Children:
- e048ece
- Parents:
- 5546f50b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/relatedwork.tex
r5546f50b r9398177 2454 2454 2455 2455 2456 \section{Algebraic Data Type} 2456 \section{OCamal} 2457 An enumerated data types is a list of named values. 2458 \begin{python} 2459 >>> type weekday = 2460 ... | Monday 2461 ... | Tuesday 2462 ... | Wednesday 2463 ... | Thursday 2464 ... | Friday 2465 ... | Saturday 2466 ... | Sunday 2467 \end{python} 2468 Enumerated data types are the simplest subset of Variants types. Because weekday is a summantion of values Monday to Sunday, 2469 enumerated data types are often call a sum type in turns of functional programming paradigm. 2470 2471 The values defined in an enumerated type are called data constructors. The data constructors of an enumerated data type 2472 takes no value as parameter. They are known as 0-arity constructor. 2473 2474 A more generic variant type has n-ary constructors. 2475 \begin{python} 2476 >>> type color = 2477 ... | Red 2478 ... | Green of string 2479 ... | Blue of int * bool 2480 \end{python} 2481 The color type can be constructed as a combination of a value from an int and a blue, using 2482 the Blue data constructor. Mathematically, a Blue value is a Cartesian product of the int type and the bool. 2483 Color type is a summation of a nullary type, an unary product type, and a cross product of int and bool. The 2484 OCamal variant type can be create as a sum of product of different types. 2485 2486 A variant type can have a recursively definition. 2487 \begin{python} 2488 >>> type stringList = 2489 ... | Empty 2490 ... | Pair of string * list 2491 \end{python} 2492 2493 OCaml's variant types are recusrive sum of product of types, which are known as Algebraic Data Types. Programming languages 2494 follows the functional programming paradigm often supports algebraic data types, and supports Pattern Matching against 2495 algebraic data type. 2496 2497 \begin{python} 2498 >>> let take_class = function 2499 ... | Monday | Wednesday -> "CS442" 2500 ... | Tuesday | Thursday -> "CS343" 2501 ... | Friday -> "Tutorial" 2502 ... | _ -> "Take a break" 2503 \end{python} 2504 2505 The function a weekday as parameter, and returns "CS442" if the weekday value is Monday or Wednesday, "CS343" 2506 if the value is Tuesday or Thursday, "Tutorial" if it is Friday. The @_@ is wildcard, and it can match any weekday value. 2507 If the value is Saturday or Sunday, which are not matched by the previous cases, it will be matched by the @_@ and the 2508 function returns "Take a break". 2509 2510 Values of a product type can be named. 2511 \begin{python} 2512 >>> let check_color = match c with 2513 ... | Red -> "Red" 2514 ... | Green g -> g 2515 ... | Blue i, _ -> int_of_string i 2516 \end{python} 2517 2518 Recurisve function are often used to pattern match against a recurisve variant type. 2519 \begin{python} 2520 >>> let rec len_of_string_list = match l with 2521 ... | Empty -> 0 2522 ... | _ * r -> 1 + len_of_string_list r 2523 \end{python}
Note: See TracChangeset
for help on using the changeset viewer.