Skip to content

Instantly share code, notes, and snippets.

@paolino
Created October 3, 2011 17:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paolino/1259637 to your computer and use it in GitHub Desktop.
Save paolino/1259637 to your computer and use it in GitHub Desktop.
Using Alternative <|> to fold nested if
import Control.Monad
import Control.Applicative
checkProperty = undefined
getDmgMult t elem = maybe 1 id $ do
e <- elem
let check f = checkProperty t ((==e) . f) >>= guard
(check succ >> return 2) `mplus` (check pred >> return 0.5)
getDmgMult t elem = maybe 1 id $ do
e <- elem
let check f = checkProperty t ((==e) . f) >>= guard
2 <$ check succ <|> 0.5 <$ check pred
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment