Write the codes in HASKELL. For example: *Main> :t splits splits :: (Ord a) => [
ID: 3823767 • Letter: W
Question
Write the codes in HASKELL.
For example:
*Main> :t splits splits :: (Ord a) => [a] -> [([a], [a])]
*Main> splits "abc"
[("c","ab"),("b","ac"),("bc","a"),("a","bc"),("ac","b"),("ab","c")]
2. The function argmin takes a function f and a list xs as arguments and returns the element of the list x such that f applied to x has minimum value. Write argmin.
For example:
*Main> :t argmin
argmin :: (Ord a) => (t -> a) -> [t] -> t
*Main> argmin length ["ABC","EF","GHIJ","K"]
"K"
Thank you.
nExplanation / Answer
2.
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_
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.