支持Elm中的功能模式匹配

loo*_*oky 5 functional-programming pattern-matching elm

在Elm中有一种方法可以将函数的参数与Haskell中的多个定义进行模式匹配吗?

Haskell的例子:

factorial :: Int ->
factorial 0 = 1
factorial n = n * factorial (n - 1)
Run Code Online (Sandbox Code Playgroud)

hal*_*bra 8

在Elm中没有相同的语法.

实现类似行为的最简单方法是使用模式匹配case语句.

请考虑以下示例:

factorial : Int -> Int
factorial n =
  case n of
    0 ->
      1
    _ ->
      n * factorial (n - 1)
Run Code Online (Sandbox Code Playgroud)

_上述从实施例用来作为通配符匹配任何图案,在此情况下,它是从不同的任意整数0