OCaml - sgml/signature GitHub Wiki

Structured Pattern Matching

type token = 
  | Noun of string 
  | Verb of string 
  | Adjective of string;;

let categorize_word word =
  match word with
  | "run" | "jump" -> Verb word
  | "cat" | "dog" -> Noun word
  | "happy" | "sad" -> Adjective word
  | _ -> failwith "Unknown word";;
type entity = 
  | Person of string
  | Location of string
  | Date of string;;

let recognize_entity text =
  match text with
  | "John Doe" -> Person text
  | "New York" -> Location text
  | "2025-04-15" -> Date text
  | _ -> failwith "Unrecognized entity";;