Hi, the simple way to implement this is to use existing functionality from the F# library, in particular Seq.zip3:

1
2
let seq_map3 f xs1 xs2 xs3 = 
  Seq.zip3 xs1 xs2 xs3 |> Seq.map (fun (a,b,c) -> f a b c)

Implementing this efficiently directly is quite difficult. In fact, you'd have to use the underlying representation of sequences (which is the .NET IEnumerable type). You can look at the F# library source code to see how this can be done. Also note that using Seq.hd and Seq.skip is quite inefficient, so it is better to avoid using these function.

In fact, this is somthing that Don Syme (the F# language designer) mentioned to me just recently as an interesting problem, so if you run into problems like this, definitely menin it on the hub! It will be a useful feedback.

By on 11/15/2008 5:09 PM ()

Well, the inner helper function is unnecessary; I might write

1
2
3
4
5
6
7
8
9
10
11
let rec seq_map3 f xs1 xs2 xs3 = 
    seq {
        if (not (Seq.is_empty xs1)) then
            yield f (Seq.hd xs1) (Seq.hd xs2) (Seq.hd xs3)
            yield! seq_map3 f (Seq.skip(1) xs1) (Seq.skip(1) xs2) (Seq.skip(1) xs3) 
        else
            yield! Seq.empty 
    } 

let oneTwoThree = [1;2;3] |> Seq.of_list 
printfn "%A" (seq_map3 (fun x y z -> 100*x + 10*y + z) oneTwoThree oneTwoThree oneTwoThree)

instead.

By on 11/15/2008 5:06 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