lookyhooky
lookyhooky

Reputation: 222

Support for functional pattern matching in Elm

In Elm is there a way to pattern match the arguments of a function to multiple definitions like in Haskell?

Example from Haskell:

factorial :: Int ->
factorial 0 = 1
factorial n = n * factorial (n - 1)

Upvotes: 5

Views: 441

Answers (1)

halfzebra
halfzebra

Reputation: 6797

There is no equivalent of that syntax in Elm.

The easiest way to achieve similar behavior would be using pattern matching with case statement.

Please consider the following example:

factorial : Int -> Int
factorial n =
  case n of
    0 ->
      1
    _ ->
      n * factorial (n - 1)

The _ from the example above serves as a wildcard to match any pattern, in this case, it's any integer different from 0

Upvotes: 8

Related Questions