Swift

Swift

Installation

Ubuntu
apt install swift
Alma Linux
dnf install swift-lang
Arch Linux
yay -S swift-bin
Binary
# Download swift-{version}-RELEASE-{os}.tar.gz from https://www.swift.org/
tar xzf swift-{version}-RELEASE-{os}.tar.gz
cd swift-{version}-RELEASE-{os}
mkdir /usr/local/swift-{version}
cp usr/* /usr/local/swift-{version}
export PATH=/usr/local/swift-{version}/bin:$PATH

Examples

Fibonacci Numbers
// Fibonacci Numbers
func fib(num n: Int) -> (Int) {
    if n < 2 {
        return n
    } else {
        return fib(num: n - 2) + fib(num: n - 1)
    }
}

let n = Int(CommandLine.arguments.dropFirst().first ?? "")!
print(String(fib(num: n)))

/*
example
    JITmode:
        swift fib_swift.swift 39
    compile:
        swiftc -O -o fib_swift fib_swift.swift
    run:
        ./fib_swift 39
*/