Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point free monadic expression

Tags:

haskell

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.

like image 644
Evan Sebastian Avatar asked Feb 22 '26 15:02

Evan Sebastian


1 Answers

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.

like image 182
András Kovács Avatar answered Feb 24 '26 19:02

András Kovács



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!