3.10. Practice Problem

Write a recursive function using the State monad which returns the longest String in a list of Strings. The state that you will carry will be the "best so far" and the type of the function will be:

longestStringM :: [String] -> State String ()

Since the CIS department machines have no Haskell interpreter, I will be your interpreter. Ask questions about things you don't understand. Use paper or an editor.

In this case, we will write a wrapper function which extracts the element from the monad. Is such a function possible with the IO monad?

longestString :: [String] -> String
longestString s = execState (longestStringM s) ""