Sha256: 9cfec15e786bc6127ee0369f94b3f420624bc6852455b448ad0d2e1de470fc29
Contents?: true
Size: 786 Bytes
Versions: 277
Compression:
Stored size: 786 Bytes
Contents
{-# LANGUAGE OverloadedStrings #-} module WordProblem (answer) where import Data.Text (pack) import Data.List (foldl') import Control.Applicative ((<|>)) import Data.Attoparsec.Text ( Parser, signed, decimal, space, maybeResult, parse, many' ) answerParser :: Parser Int answerParser = do n <- "What is " *> signed decimal ops <- many' (space *> operation) "?" *> pure (foldl' (flip ($)) n ops) answer :: String -> Maybe Int answer = maybeResult . parse answerParser . pack operation :: Parser (Int -> Int) operation = (flip <$> operator) <* space <*> signed decimal operator :: Parser (Int -> Int -> Int) operator = "plus" *> pure (+) <|> "minus" *> pure (-) <|> "multiplied by" *> pure (*) <|> "divided by" *> pure div
Version data entries
277 entries across 277 versions & 1 rubygems