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.119 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.118 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.117 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.116 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.115 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.114 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.113 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.111 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.110 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.109 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.108 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.107 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.106 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.105 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.104 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.103 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.102 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.101 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.100 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs
trackler-2.2.1.99 tracks/haskell/exercises/wordy/examples/success-standard/src/WordProblem.hs