user1072706
user1072706

Reputation: 573

first-class function

Lets say I have a function

 let makeMonitoredFun f =

     let c = ref 0

     (fun x -> c := !c+1; printf "Called %d times.\n" !c; f x);;

Why I am not allowed to do this.

   let mrev = makeMonitoredFun List.rev

Upvotes: 1

Views: 206

Answers (2)

ChaosPandion
ChaosPandion

Reputation: 78262

I was able to run the following code as the compiler could infer the type of listRevCounter.

let makeMonitoredFun f =
    let c = ref 0
    (fun x -> c := !c+1; printf "Called %d times.\n" !c; f x)

let listRevCounter = makeMonitoredFun List.rev

let revList = listRevCounter [ 1; 2; 3 ]

Upvotes: 1

John Palmer
John Palmer

Reputation: 25516

So presumably you are referring to the value type restriction you get when you try to compile the code. If you add a type annotation it will work fine/. For the details of value type errors see this article http://blogs.msdn.com/b/mulambda/archive/2010/05/01/value-restriction-in-f.aspx from one of the F# developers

Upvotes: 5

Related Questions