Last active
August 29, 2015 14:11
-
-
Save TrevorBasinger/c84aa0899b159cf3b898 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Test where | |
import Data.Foldable (traverse_) | |
import Control.Monad.Eff | |
import Control.Monad.State | |
import Control.Monad.State.Class | |
import Control.Monad.Reader | |
import Control.Monad.Reader.Class | |
import Data.String (length, split) | |
import Data.Array ((..)) | |
import Debug.Trace | |
sumArray :: [Number] -> State Number Unit | |
sumArray = traverse_ $ \n -> modify (\sum -> sum + n ) | |
paramValue :: String -> Number | |
paramValue "(" = 1 | |
paramValue ")" = -1 | |
paramValue _ = 0 | |
isZero :: Number -> Boolean | |
isZero 0 = true | |
isZero _ = false | |
testState :: Number -> Number -> Number | |
testState x y | y >= 0 = x + y | |
testState _ _ = -1 | |
testParens :: String -> Boolean | |
testParens = isZero <<< flip execState 0 <<< traverse_ (\c -> modify $ testState (paramValue c) ) <<< split "" | |
main :: forall eff. Eff ( trace :: Trace | eff) Unit | |
main = do | |
-- trace $ show $ execState (sumArray (1 .. 11)) 0 | |
trace $ show $ testParens "(())())(" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment