Sha256: fff684e8107fb458324354d478469bb97b6450bdd0f5e19b4e9dd06c9f8d5e12
Contents?: true
Size: 786 Bytes
Versions: 119
Compression:
Stored size: 786 Bytes
Contents
{-# LANGUAGE OverloadedStrings #-} module WordProblem (answer) where import Data.Functor (($>)) 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) "?" $> 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" $> (+) <|> "minus" $> (-) <|> "multiplied by" $> (*) <|> "divided by" $> div
Version data entries
119 entries across 119 versions & 1 rubygems