Question: F# Programming: Write an F# program lengthsort to sort a list of lists according the lengths of the element lists such that short lists first,
F# Programming:
Write an F# program lengthsort to sort a list of lists according the lengths of the element lists such that short lists first, longer lists later.
> lengthsort [[2;3];[1;2;3;4];[1;2;4;5;6];[2;3;4];[3]];; val it : int list list =
[[3]; [2; 3]; [2; 3; 4]; [1; 2; 3; 4]; [1; 2; 4; 5; 6]]
Hint: Use List.map to construct a tuple list from the given list such that the first element is the length of the element list, e.g [[2;3];[1;2;3;4];[1;2;4;5;6];[2;3;4];[3]] becomes [(2,[2;3]);(4,[1;2;3;4]);(5,[1;2;4;5;6]);(3,[2;3;4]);(1,[3])]. Write a function to sort the list based on the 1st element of the tuple, and then drop the 1st element in the tuples to obtain the final result.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
