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

Version Path
trackler-2.2.1.180 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.179 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.178 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.177 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.176 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.175 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.174 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.173 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.172 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.171 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.170 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.169 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.167 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.166 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.165 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.164 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.163 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.162 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.161 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.160 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs