From cdb38e1c726e11981bbaeac2a7ed209059d7093f Mon Sep 17 00:00:00 2001 From: Aaro Koivukangas Date: Tue, 16 Jan 2024 19:20:59 +0200 Subject: [PATCH] ex9. joinToLength done.. finally --- exercises/Set3a.hs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/exercises/Set3a.hs b/exercises/Set3a.hs index f89a48f..c63f194 100644 --- a/exercises/Set3a.hs +++ b/exercises/Set3a.hs @@ -195,7 +195,17 @@ bomb x = Right (x-1) -- Hint! This is a great use for list comprehensions joinToLength :: Int -> [String] -> [String] -joinToLength = todo +joinToLength n xs = filter (\str -> length str == n) (allCombinations xs) + + +allCombinations :: [String] -> [String] +allCombinations xs = merge (map (`allCombinationsWithStr` xs) xs) [] + where + merge :: [[a]] -> [a] -> [a] + merge (x:xs) acc = merge xs (x ++ acc) + merge _ acc = acc + allCombinationsWithStr :: String -> [String] -> [String] + allCombinationsWithStr x = map (x++) ------------------------------------------------------------------------------ -- Ex 10: implement the operator +|+ that returns a list with the first