2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1,13 @@
calcRPN :: String -> [Double]
calcRPN = foldl interprete [] . words
interprete s x
| x `elem` ["+","-","*","/","^"] = operate x s
| otherwise = read x:s
where
operate op (x:y:s) = case op of
"+" -> x + y:s
"-" -> y - x:s
"*" -> x * y:s
"/" -> y / x:s
"^" -> y ** x:s

View file

@ -0,0 +1,6 @@
calcRPNLog :: String -> ([Double],[(String, [Double])])
calcRPNLog input = mkLog $ zip commands $ tail result
where result = scanl interprete [] commands
commands = words input
mkLog [] = ([], [])
mkLog res = (snd $ last res, res)

View file

@ -0,0 +1,7 @@
import Control.Monad (foldM)
calcRPNIO :: String -> IO [Double]
calcRPNIO = foldM (verbose interprete) [] . words
verbose f s x = write (x ++ "\t" ++ show res ++ "\n") >> return res
where res = f s x

View file

@ -0,0 +1,10 @@
class Monad m => Logger m where
write :: String -> m ()
instance Logger IO where write = putStr
instance a ~ String => Logger (Writer a) where write = tell
verbose2 f x y = write (show x ++ " " ++
show y ++ " ==> " ++
show res ++ "\n") >> return res
where res = f x y

View file

@ -0,0 +1,2 @@
calcRPNM :: Logger m => String -> m [Double]
calcRPNM = foldM (verbose interprete) [] . words

View file

@ -1,15 +0,0 @@
import Data.List (elemIndex)
-- Show results
main = mapM_ (\(x, y) -> putStrLn $ x ++ " ==> " ++ show y) $ reverse $ zip b (a:c)
where (a, b, c) = solve "3 4 2 * 1 5 - 2 3 ^ ^ / +"
-- Solve and report RPN
solve = foldl reduce ([], [], []) . words
reduce (xs, ps, st) w =
if i == Nothing
then (read w:xs, ("Pushing " ++ w):ps, xs:st)
else (([(*),(+),(-),(/),(**)]!!o) a b:zs, ("Performing " ++ w):ps, xs:st)
where i = elemIndex (head w) "*+-/^"
Just o = i
(b:a:zs) = xs