I think the F# spec has a resonable explation, section 6.6.4

([link:research.microsoft.com]

function

| 1,j -> printfn "j = %d" j

| _ -> printfn "no match"

Is syntactic sugar for:

fun x ->

match x with

| 1,j -> printfn "j = %d" j

| _ -> printfn "no match"

Personally I'm not too keen on the use of the 'function' key word as it makes things more obscure for only the sake of a few characters saving, but there are some programmers who like it and it can be quite useful if all a function does is match over a union type and make a very simple transformation.

Cheers,

Rob

By on 12/10/2008 8:47 AM ()

Robert -

I had gotten that feeling that you didn't care for it too much from your book. LOL.

One question I have about the example given is are you matching a tuple (i, j) in the first rule as the manual says it is syntactic sugar for a singe argument lambda expression followed by immediate matches on the argument.

By on 12/10/2008 10:26 AM ()

Yep, that's correct, a tuple can be pact into a single argument. Perhaps it's more clear if the tuple is unpacked explicitly:

fun x ->

let i, j = x in

match (i, j) with

| 1,j -> printfn "j = %d" j

| _ -> printfn "no match"

So this function will have type:

(int * int) -> string

So if can be used with a single argument as long as this argument is a tuple of ints, see what I mean?

Cheers,

Rob

By on 12/11/2008 1:02 AM ()

also fun can only do basic pattern matching. with function you can do, for example:

1
ls |> List.map (function ComplicatedPattern x -> x)

the bar doesn't seem to be required for single cases which is nice

By on 12/19/2008 6:27 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper