Ignore:
Timestamp:
Mar 11, 2024, 2:22:19 PM (4 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
446740a
Parents:
e048ece
Message:

Fix ocaml code

File:
1 edited

Legend:

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

    re048ece r6c8b76b  
    24772477... | Red
    24782478... | Green of string
    2479 ... | Blue of int * bool
     2479... | Blue of int * float
    24802480\end{python}
    24812481The color type can be constructed as a combination of a value from an int and a blue, using
     
    24882488>>> type stringList =
    24892489... | Empty
    2490 ... | Pair of string  * list
     2490... | Pair of string * stringList
    24912491\end{python}
    24922492
     
    24962496
    24972497\begin{python}
    2498 >>> let take_class = function
     2498>>> let take_class = functio
    24992499... | Monday | Wednesday -> "CS442"
    25002500... | Tuesday | Thursday -> "CS343"
    25012501... | Friday -> "Tutorial"
    2502 ... | _ -> "Take a break"
     2502... | _ -> "Take a break";;
    25032503\end{python}
    25042504
     
    25102510Values of a product type can be named.
    25112511\begin{python}
    2512 >>> let check_color = match c with
     2512>>> let check_color (c: color): string =
     2513... match c with
    25132514... | Red -> "Red"
    25142515... | Green g -> g
    2515 ... | Blue i, _ -> int_of_string i
     2516... | Blue (i, f) -> string_of_int i ^ string_of_float f;;
    25162517\end{python}
    25172518
    25182519Recurisve function are often used to pattern match against a recurisve variant type.
    25192520\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
    25212523... | Empty -> 0
    2522 ... | _ * r -> 1 + len_of_string_list r
    2523 \end{python}
     2524... | Pair (_ , r) -> 1 + len_of_string_list r
     2525\end{python}
Note: See TracChangeset for help on using the changeset viewer.