Inner lambdas that return unit don't get tail-call-ified, due to some weird interactions with 'unit' versus 'void'. You can work around it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let merge1 (l, r) =
    let merged = Array.zeroCreate ((List.length l) + (List.length r))
    let rec merge index (l, r) = 
        let complete = List.iteri (fun i -> Array.set merged (index + i))
        match (l, r) with
        | (_, []) -> complete l; 0 | ([], _) -> complete r; 0 // note, return dummy 0
        | (hl::tl, hr::tr) -> 
            let set x = 
                Array.set merged index x
                merge (index + 1)
            if hl < hr then 
                set hl (tl, r) 
            else 
                set hr (l, tr)
    merge 0 (l, r) |> ignore // ignore dummy
    List.ofArray merged

so that the recursive bit returns 'int' rather than 'unit'.

By on 10/10/2010 3:14 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