F# and Swift

I’m looking at Apple’s Swift language and it’s interesting to see how similar it is to F# (which itself is similar to Scala and Haskell). I think this is all in reaction to the trouble we’re having in general with pure object-oriented programming and the advantages of a functional (or at least hybrid) approach in the age of distributed systems.

Value declaration

This is some F# for assigning variables:

let someConstant = 10
let mutable someVariable = 1.23
let specifyTypeExplicitly : String = "blah"

and in Swift:

let someConstant = 10 
var someVariable = 1.23
var specifyTypeExplicitly : Double = "blah"

let mutable for var, otherwise the same. Types are generally inferred but can be set explicitly.

Both languages have tuples and dictionaries assigned in basically the same way.

Pattern matching

Instead of F#’s match, Swift puts its matching inside a traditional switch statement.

//let rgba = ( 1.0, 1.0, 1.0, 1.0 ) "white"
//let rgba = ( 0.4, 0.4, 0.4, 1.0 ) "gray"
//let rgba = ( 0.0, 0.6, 0.8, 1.0 ) "blue is 0.8"
let rgba = ( 0.0, 0.6, 0.8, 1.0 )
switch rgba {
case ( 1.0, 1.0, 1.0, 1.0 ):
    print( "white" )
case let ( r, g, b, 1.0) where r==g && g==b:
    print("gray")
case (0.0, 0.5...1.0, let b, _):
    print("blue is \(b)")
default:
    break
}

In F#, this would be something like (this is from memory):

//let rgba = ( 1.0, 1.0, 1.0, 1.0 ) "white"
//let rgba = ( 0.4, 0.4, 0.4, 1.0 ) "gray"
//let rgba = ( 0.0, 0.6, 0.8, 1.0 ) "blue is 0.800000"
let rgba = ( 0.0, 0.6, 0.8, 1.0 )
match rgba with
| ( 1.0, 1.0, 1.0, 1.0 ) ->
    printfn "white"
| ( r, g, b, 1.0 ) when ( r = g ) && ( g = b ) ->
    printfn "gray"
| ( 0.0, g, b, _ ) when ( g >= 0.5 ) && ( g <= 1.0 ) ->
    printfn "blue is %f" b 
| _ -> ()

Some and None

Swift and F# both support the Some and None keywords for optional values. e.g.

var x : Int? = nil
switch x {
case .Some( let value ):
    print("x has a value")
case .None:

In F#:

let x : int option = None
match x with
| Some(value) ->
    printfn "x has a value"
| None ->
    printfn "x is nil"

Currying

In my first look at the language, currying doesn’t seem quite as nice.
Here’s some F#:

let add a b = a + b
let add2 = add 2
let result = add2 3 // 5

In Swift (in the tersest way I know how at present) :

let add : (Int,Int) -> Int = { $0 + $1 }
let add2 = { add( 2, $0 ) }
let result = add2( 3 ) // 5

Another way (according to this) is to make the add function return a function like in the example below, but then doing a call to the initial function does not look like a standard function call:

func add (a:Int)(_ b:Int)-> Int {
    return a + b
}
add(2)(3) // not add(2,3) // 5
let add2 = add(2)
add2(3) // 5

Conclusion

This is a very shallow comparison. I just thought it was interesting.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s