'Haskell-Find the shortest word in the list
minimumBy
takes as the first parameter a function that should be used to compare two elements.
comparing
is a higher-order function that can be used to transform a function of type a -> b(where b is the type that can be compared, i.e. Ord b => a -> b
import Data.List (maximumBy)
import Data.Ord (comparing)
findShortestestWord :: String -> String
findShortestWord s = minimumBy (comparing length) (words s)
But an error in the compiler gives
"Syntax error in import declaration (unexpected symbol
"Data.List")"
Solution 1:[1]
Your code has wrong indentation, here is working example
import Data.List (minimumBy)
import Data.Ord (comparing)
findShortestWord :: String -> String
findShortestWord s = minimumBy (comparing length) (words s)
main = getLine >>= print . findShortestWord
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ianfun |