[Haskell-cafe] extending Do notation

silvio silvio.frischi at gmail.com
Tue May 20 12:58:49 UTC 2014


#The problem

> grep -r ">>=" base | head -3
getExecutablePath = _NSGetExecutablePath >>= realpath
then peek p_argv >>= peek >>= peekFilePath
... = getFileSystemEncoding >>= \enc -> GHC.withCString enc fp f

> grep -r "flip" base | head -3
... (o,n,[]  ) -> return (foldl (flip id) defaultOptions o, n)
peekCString s = getForeignEncoding >>= flip GHC.peekCString s
peekCStringLen s = getForeignEncoding >>= flip GHC.peekCStringLen s

I don't know how often i've seen this one
... >>= flip ...
when really we it should have been
... {...}

Only because we can't just execute an IO action and fill in the result.
And this list doesn't even include examples where the programmer had to
resort to making a pointless temp variable just to avoid using too many
complicated functions.

#The solution

> grep -r ">>=" base | head -3
getExecutablePath = _do realpath { _NSGetExecutablePath }
then peekFilePath { peek {peek p_argv} }
... = do GHC.withCString {getFileSystemEncoding} fp f

> grep -r "flip" base | head -3
... (o,n,[]  ) -> return (foldl (flip id) defaultOptions o, n) -- leave
peekCString s =  GHC.peekCString {getForeignEncoding} s
peekCStringLen s = GHC.peekCStringLen {getForeignEncoding} s

I think there is no use of {} in haskell that does not come after a key
word but has an expression inside. But it could be some other syntax too.

silvio


More information about the Haskell-Cafe mailing list