Multiple Patterns in 1 case

前端 未结 3 1923
囚心锁ツ
囚心锁ツ 2021-01-18 08:40

In SML, is it possible for you to have multiple patterns in one case statement?

For example, I have 4 arithmetic operators express in string, \"+\", \"-\", \"*

3条回答
  •  孤街浪徒
    2021-01-18 09:35

    In Standard ML, no. In other dialects of ML, such as OCaml, yes. You may in some cases consider splitting pattern matching up into separate cases/functions, or skip pattern matching in favor of a shorter catch-all expression, e.g.

    if str = "+" orelse str = "-" then "PLUS MINUS" else
    if str = "*" orelse str = "/" then "MULT DIV" else ...
    

提交回复
热议问题