Pattern matching in Elixir

Elixir is a ruby like language built on top of the Erlnag VM.It has some really nice features which makes writing recursive functions a breeze.Here is a defacto example to write a factorial program in elixir.

As we can see having guards and pattern matching makes recursive functions a breeze

Comparing that too ruby,the LOC are more but the code is more readable.

Comparing it to some ugly javascript.

  • Some more:

    func fact(n: Int) -> Int {
    return (n < 1 ? 1 : n * fact(n – 1))
    def fact(n):
    return 1 if n < 1 else n * fact(n-1)

    • I like how elixir separates concerns at the function level.I think swift has some pattern matching have to look at it in more detail.

      • Yeah, swift has that for switch cases:

        1> func fact(n: Int) -> Int {
        2. switch (n) {
        3. case 0…1:
        4. return 1
        5. default:
        6. return n * fact(n-1)
        7. }
        8. }