3 ms·
They are useful for recursion: def factorial(0), do: 1 def factorial(n) when n > 0, do: n * factorial(n - 1) Otherwise it comes down to a matter of ta
by sodapopcan 3y ago
They are useful for recursion:
def factorial(0), do: 1
def factorial(n) when n > 0, do: n * factorial(n - 1)
Otherwise it comes down to a matter of taste. Sometimes they read nicer than a `case`, sometimes a `case` is better.
Like I would favour:
def zero?(0), do: true
def zero?(_), do: false
over the case version, for example, but that's just me.
- sodapopcan 3y ago...and, just to be "that guy" to myself before anyone else does over my contrived example: "Or just:" def zero?(n), do: n == 0