Question: Using F# 1) Write an F# function interleave(xs,ys) that interleaves two lists: > interleave ([1;2;3],[4;5;6]);; val it : int list = [1; 4; 2; 5;

Using F#

1) Write an F# function interleave(xs,ys) that interleaves two lists:

 > interleave ([1;2;3],[4;5;6]);; val it : int list = [1; 4; 2; 5; 3; 6] 

Assume that the two lists have the same length.

2) Write an F# function cut xs that cuts a list into two equal parts:

 > cut [1;2;3;4;5;6];; val it : int list * int list = ([1; 2; 3], [4; 5; 6]) 

Assume that the list has even length.

To implement cut, first define an auxiliary function gencut(n, xs) that cuts xs into two pieces, where n gives the size of the first piece:

 > gencut(2, [1;3;4;2;7;0;9]);; val it : int list * int list = ([1; 3], [4; 2; 7; 0; 9]) 

Paradoxically, although gencut is more general than cut, it is easier to write! (This is an example of Polya's Inventor's Paradox: "The more ambitious plan may have more chances of success.")

Another Hint: To write gencut efficiently, it is quite convenient to use F#'s local let expression (as in the cos_squared example in the Notes).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!