Changeset 6c8b76b
- Timestamp:
- Mar 11, 2024, 2:22:19 PM (9 months ago)
- Branches:
- master
- Children:
- 446740a
- Parents:
- e048ece
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/relatedwork.tex
re048ece r6c8b76b 2477 2477 ... | Red 2478 2478 ... | Green of string 2479 ... | Blue of int * bool2479 ... | Blue of int * float 2480 2480 \end{python} 2481 2481 The color type can be constructed as a combination of a value from an int and a blue, using … … 2488 2488 >>> type stringList = 2489 2489 ... | Empty 2490 ... | Pair of string * list2490 ... | Pair of string * stringList 2491 2491 \end{python} 2492 2492 … … 2496 2496 2497 2497 \begin{python} 2498 >>> let take_class = functio n2498 >>> let take_class = functio 2499 2499 ... | Monday | Wednesday -> "CS442" 2500 2500 ... | Tuesday | Thursday -> "CS343" 2501 2501 ... | Friday -> "Tutorial" 2502 ... | _ -> "Take a break" 2502 ... | _ -> "Take a break";; 2503 2503 \end{python} 2504 2504 … … 2510 2510 Values of a product type can be named. 2511 2511 \begin{python} 2512 >>> let check_color = match c with 2512 >>> let check_color (c: color): string = 2513 ... match c with 2513 2514 ... | Red -> "Red" 2514 2515 ... | Green g -> g 2515 ... | Blue i, _ -> int_of_string i2516 ... | Blue (i, f) -> string_of_int i ^ string_of_float f;; 2516 2517 \end{python} 2517 2518 2518 2519 Recurisve function are often used to pattern match against a recurisve variant type. 2519 2520 \begin{python} 2520 >>> let rec len_of_string_list = match l with 2521 >>> let rec len_of_string_list(l: stringList): int = 2522 ... match l with 2521 2523 ... | Empty -> 0 2522 ... | _ * r-> 1 + len_of_string_list r2523 \end{python} 2524 ... | Pair (_ , r) -> 1 + len_of_string_list r 2525 \end{python}
Note: See TracChangeset
for help on using the changeset viewer.