Advanced Pattern Matching

Fibonacci

Fibonacci

Also, see how beautiful an implementation of the fibonacci algorithm looks with pattern matching.

func fibonacci(i: Int) -> Int {
    switch(i) {
    case let n where n <= 0: return 0
    case 0, 1: return 1
    case let n: return fibonacci(n - 1) + fibonacci(n - 2)
    }
}

print(fibonacci(8))

Since we're doing recursion here, this will fail to work with sufficiently large numbers (you'll see the dreaded stack overflow error)