[Haskell-cafe] Can I write this function without doing the action on a different line?

Karl Voelker karl at karlv.net
Sun Apr 6 07:15:05 UTC 2014


On Sat, Apr 5, 2014, at 11:31 PM, Matt Joiner wrote:

 1. packageBase <- do
 2.     curDir <- getCurrentDirectory
 3.
    return . last . splitDirectories . normalise . joinPath $ curDir:ma
    ybeToList package



Sure. The most obvious transformation to me would have these three
steps:



1. The right argument of (:) isn't coming from a monadic action, so you
can extract it as a section and move it to your big long chain of
compositions:



return . last . splitDirectories . normalise . joinPath . (:
maybeToList package) $ curDir



2. Get rid of the binding of curDir:



getCurrentDirectory >>= return . last . splitDirectories . normalise .
joinPath . (: maybeToList package)



3. Notice that the right argument of (>>=) really isn't using your
monad, so you could use fmap or (<$>) (assuming this is a well-behaved
monad which also has a Functor instance):



last . splitDirectories . normalise . joinPath . (: maybeToList
package) <$> getCurrentDirectory



Fair warning: I'm tired and not type-checking this. But it looks right
to me.



-Karl
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140406/2a1be49b/attachment.html>


More information about the Haskell-Cafe mailing list