Evan Sebastian
Evan Sebastian

Reputation: 1744

Point free monadic expression

Consider this expression (taken from Real World Haskell ch. 8, which I try to simplify)

isElfFile :: FilePath -> IO Bool
isElfFile path = return . hasElfMagic =<< L.readFile path

How do I make a point-free version of this function? I tried using its the other bind operation >>=, lifting hasElfMagic, but none seems to be working.

Upvotes: 3

Views: 431

Answers (1)

Andr&#225;s Kov&#225;cs
Andr&#225;s Kov&#225;cs

Reputation: 30103

It's simpler without binding here:

isElfFile path = return . hasElfMagic =<< L.readFile path
isElfFile path = fmap hasElfMagic (L.readFile path)
isElfFile      = fmap hasElfMagic . L.readFile 

But of course also doable with =<<:

isElfFile path = return . hasElfMagic =<< L.readFile path
isElfFile path = (=<<) (return . hasElfMagic) (L.readFile path)
isElfFile      = (=<<) (return . hasElfMagic) . L.readFile
isElfFile      = (return . hasElfMagic =<<) . L.readFile

In general, it helps to convert infix functions into prefix form before trying make things point-free.

Upvotes: 8

Related Questions