From 010404abf1321443677efc91d21eb7f93c91a324 Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Sat, 11 Mar 2023 17:31:29 +0100 Subject: 3_b_6: Add solution --- README.md | 2 +- ch03/3_b_6.hs | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 ch03/3_b_6.hs diff --git a/README.md b/README.md index 9218b56..0d39a74 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ more visible in the list the first exercise of a group is in bold italics. | 3_b_3 | yes | | | | 3_b_4 | yes | | | | 3_b_5 | yes | | | -| 3_b_6 | | 70 | | +| 3_b_6 | yes | 70 | | | 3_b_7 | | | | | 3_b_8 | | | | | 3_b_9 | | | | diff --git a/ch03/3_b_6.hs b/ch03/3_b_6.hs new file mode 100644 index 0000000..74d46ac --- /dev/null +++ b/ch03/3_b_6.hs @@ -0,0 +1,25 @@ +-- Create a function that sorts a list of lists based on the length of each +-- sublist. (You may want to look at the sortBy function from the Data.List +-- module.) + +import Data.List + +myCompare :: [a] -> [a] -> Ordering +myCompare a b = if len_a < len_b + then LT + else if len_a == len_b + then EQ + else GT + where len_a = length a + len_b = length b + +mySort :: [[a]] -> [[a]] +mySort xs = Data.List.sortBy myCompare xs + +-- ghci> :l 3_b_6.hs +-- [1 of 1] Compiling Main ( 3_b_6.hs, interpreted ) +-- Ok, one module loaded. +-- ghci> mySort [] +-- [] +-- ghci> mySort [[2, 3], [7, 8, 9, 10], [1], [], [4, 5, 6]] +-- [[],[1],[2,3],[4,5,6],[7,8,9,10]] -- cgit v1.2.3