Type inference in pattern match over list
I'm stuck with trying to create a function that will do the following: 

    let p = [1, 2, 4, 2, 1]
    let v = 8

then I want to go over the elements in `a` until I find the index `i` of the one where all elements up to and including `i` are greater or equal than `v`. So in this case it should return 4, because 1+2+4 < 8 and 1+2+4+2 > 8. So there 4th element, value of 2 puts the total over 8. 

(some background: I want to do monte carlo, where I have a list of probabilies that add up to 1. Then I pass a random number between 0 and 1 to have it pick one of the possible future states. Simple example here with ints)

What I have so far is this:

    let p = [1, 2, 4, 2, 1]
    
    let pick (v:int) (l:int list) = 
        let rec sub (i:int) (acc:int) (l2:int list) =
            match l2 with
            | h::t -> if acc+h >= v then i else sub(i+1, acc+h, t)
            | _ -> failwith "total list didn't amount to " + v
        sub 0 0 l
        
    pick 8 p


But it gives the following error on the bit `sub(i+1, acc+h, t)`:

    error FS0001: This expression was expected to have type
        int    
    but here has type
        int -> int list -> int 


But I'm not passing the function `sub` itself, I'm *calling* it complete with arguments and brackets and all, so why doesn't it return int, the result of the function? 

How do I make it return the result of the function?

Thanks in advance,

Gert-Jan
Type inference in pattern match over list
===

I'm stuck with trying to create a function that will do the following: 

    let p = [1, 2, 4, 2, 1]
    let v = 8

then I want to go over the elements in `a` until I find the index `i` of the one where all elements up to and including `i` are greater or equal than `v`. So in this case it should return 4, because 1+2+4 < 8 and 1+2+4+2 > 8. So there 4th element, value of 2 puts the total over 8. 

(some background: I want to do monte carlo, where I have a list of probabilies that add up to 1. Then I pass a random number between 0 and 1 to have it pick one of the possible future states. Simple example here with ints)

What I have so far is this:

    let p = [1, 2, 4, 2, 1]
    
    let pick (v:int) (l:int list) = 
        let rec sub (i:int) (acc:int) (l2:int list) =
            match l2 with
            | h::t -> if acc+h >= v then i else sub(i+1, acc+h, t)
            | _ -> failwith "total list didn't amount to " + v
        sub 0 0 l
        
    pick 8 p


But it gives the following error on the bit `sub(i+1, acc+h, t)`:

    error FS0001: This expression was expected to have type
        int    
    but here has type
        int -> int list -> int 


But I'm not passing the function `sub` itself, I'm *calling* it complete with arguments and brackets and all, so why doesn't it return int, the result of the function? 

How do I make it return the result of the function?

Thanks in advance,

Gert-Jan