Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*Main> :t splits splits :: (Ord a) => [a] -> [([a], [a])] *Main> splits \"abc\"

ID: 3830800 • Letter: #

Question

*Main> :t splits

splits :: (Ord a) => [a] -> [([a], [a])]

*Main> splits "abc"

[("c","ab"),("b","ac"),("bc","a"),("a","bc"),("ac","b"),("ab","c")]

Recall that the function combinations takes a list of elements of typeclass Ord and an integer k as its arguments and returns a list of (i) length k lists representing all possible subsets of size k. The function splits is similar, except that, given a list of elements of length n, it returns a list of (i) pairs of lists. The first component of the pair represents a combination of length k. The second component represents the complementary combination of length n-k. Two combinations are complementary when their union is equal to the original list. For example,

Explanation / Answer

import Data.Ord (comparing)


wds :: [String]
wds = ["ABC", "EF", "GHIJ", "K"]

main :: IO ()
main = print $ argim (comparing length) wds

argim
:: Foldable t
=> (a -> a -> Ordering) -> t a -> a
argim cmp =
let min_ x y =
case cmp x y of
LT -> x
_ -> y
in foldr1 min_