Homework Solution: Write an F# function interleave l1 l2 (defined below with examples) that interleaves t…

    Write an F# function interleave l1 l2 (defined below with examples) that interleaves two lists of arbitrary size: > interleave [1..3] [4..6];; val it : int list = [1; 4; 2; 5; 3; 6] > interleave [1..10] [3..5];; val it : int list = [1; 3; 2; 4; 3; 5; 4; 5; 6; 7; 8; 9; 10] > interleave [3..5] [1..10];; val it : int list = [3; 1; 4; 2; 5; 3; 4; 5; 6; 7; 8; 9; 10] > interleave ["cat"; "dog"; "elephant"; "lion"] ["tiger"; "rat"];; val it : string list = ["cat"; "tiger"; "dog"; "rat"; "elephant"; "lion"]

    Expert Answer

     
    let rec interleave

    Write an F# function interleave l1 l2 (defined underneath with examples) that interleaves span catalogues of irresponsible size:

    > interleave [1..3] [4..6];;

    val it : int catalogue = [1; 4; 2; 5; 3; 6]

    > interleave [1..10] [3..5];;

    val it : int catalogue = [1; 3; 2; 4; 3; 5; 4; 5; 6; 7; 8; 9; 10]

    > interleave [3..5] [1..10];;

    val it : int catalogue = [3; 1; 4; 2; 5; 3; 4; 5; 6; 7; 8; 9; 10]

    > interleave [“cat”; “dog”; “elephant”; “lion”] [“tiger”; “rat”];;

    val it : string catalogue = [“cat”; “tiger”; “dog”; “rat”; “elephant”; “lion”]

    Expert Repartee

     

    let rec interleave = function
        | (xs, []) -> []
        | ([], ys) -> []
        | (x::xs, y::ys)-> x::y::interleave( xs, ys)