Having troubles understanding this F# code

27 views
Skip to first unread message

Alex

unread,
Oct 15, 2015, 10:25:45 PM10/15/15
to Machine Learning Projects for DotNET Developers
When I read "classify" function below, I expect that it has "pixels" as in input parameter on line 32. However on line 36, I do not see any parameter passed in to "classify" function. 
31 let train (trainingset:Observation[]) (dist:Distance) =
32     let classify (pixels:int[]) =
33         trainingset
34         |> Array.minBy (fun x -> dist (x.Pixels, pixels))
35         |> fun x -> x.Label
36     classify

I've expected to see some type of parameter on line 36, like this:
36     classify trainingImage

 What I am missing?

Thank you in advance,
PS. I am really enjoying the book.

Mathias Brandewinder

unread,
Oct 19, 2015, 6:26:56 PM10/19/15
to Machine Learning Projects for DotNET Developers
Hi Alex,

This is a great question. Let me illustrate what is going on, on a simpler example.

The following snippet creates a (silly) function that adds 42 to an integer x:

let add42 x = x + 42

The signature for that function indicates that add42 is a function that expects an integer, and returns an integer:

val add42 : x:int -> int

Now I can perfectly well do something like this:

let f = add42

... and f IS identical to add42, that is, it is a function that expects an integer, and returns back an integer.

What I am doing with train is, in essence, the same as the code below:

let createAdder (value:int) =    
    let addValue x
= x + value
    addValue

let add1
= createAdder 1

Here I create a function addValue inside createAdder, which will add a value to any input x, and return that function, which is ready to go: you can then call with any integer x, just like in the previous example with add42.

The train example does the exact same thing: it creates a classify function which expects one argument (pixels:int[]), and once that function is specified, simply returns it.

I hope the simpler example helps see what is going on - I am aware that this isn't a full explanation, and more of a repetition with less noise around it. The following blog post from F# for Fun & Profit might also be useful, it digs deeper into the why and how of how functions 'work' in F#: http://fsharpforfunandprofit.com/posts/function-values-and-simple-values/ . Hope this helps, and feel free to ask for more. 

Cheers, and... happy reading :)

Mathias

Alex

unread,
Oct 20, 2015, 4:08:48 PM10/20/15
to Machine Learning Projects for DotNET Developers
Hi Mathias,

I get it now:
   "train" returns a "function pointer" to the "classify" function; later on, "classifier" function evaluates (x.Pixels), such invoking a "classify" function on proper parameter as in highlighted code below
***  

let evaluate validationSet classifier =

validationSet

|> Array.averageBy (fun x -> if classifier x.Pixels = x.Label then 1. else 0.)

|> printfn "Correct: %.3f"

***
Thank you for the reply.
Alex
Reply all
Reply to author
Forward
0 new messages