Last active
December 20, 2015 06:29
-
-
Save cleichner/6086604 to your computer and use it in GitHub Desktop.
Revisions
-
cleichner revised this gist
Apr 15, 2015 . 9 changed files with 149 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ Copyright 2014 Chas Leichner Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ -- First Haskell version, nothing tricky, no syntactic sugar around monad -- operations. import Control.Monad import Data.Char import System.IO import System.Random -- >> :: IO a -> IO b -> IO b -- >>= :: IO a -> (a -> IO b) -> IO b -- putStrLn :: String -> IO () gameLoop :: [Int] -> Int -> Int -> IO () gameLoop (x:y:values) right rounds = flushPut "Would you like to play? y/n: " >> getLine >>= \keepPlaying -> when (map toLower keepPlaying == "y") $ let (solution, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) in flushPut (unwords ["What is", show x, opStr, show y, "? "]) >> readLn >>= \response -> let (total, message) = if solution == response then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) in putStrLn (unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1)]) >> gameLoop values total (rounds + 1) where flushPut s = putStr s >> hFlush stdout main :: IO () main = getStdGen >>= \gen -> gameLoop (randomRs (1, 100) gen) 0 0 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 charactersOriginal file line number Diff line number Diff line change @@ -16,8 +16,8 @@ gameLoop (x:y:values) right rounds = do let (total, message) = if solution == response then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) putStrLn $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1)] gameLoop values total (rounds + 1) where flushPut s = putStr s >> hFlush stdout 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ -- Collected the game state into a Game record import Control.Applicative import Control.Monad import Data.Char import System.IO import System.Random data Game = Game { values :: [Int], right :: Int, rounds :: Int } updateGame :: Bool -> Game -> Game updateGame correct Game { values = (_:_:remaining) , right = score , rounds = total } = Game { values = remaining , right = if correct then score + 1 else score , rounds = total + 1 } gameLoop :: Game -> IO () gameLoop gameState = do flushPut "Would you like to play? y/n: " keepPlaying <- ("y" ==) . map toLower <$> getLine when keepPlaying $ do let (x:y:_) = values gameState let (solution, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] correct <- (solution ==) <$> readLn let gameState' = updateGame correct gameState putStrLn $ if correct then "Correct!" else unwords ["Sorry! the correct answer is:", show solution] putStr $ unwords ["You have solved", show $ right gameState', "out of", show $ rounds gameState', "\n"] gameLoop gameState' where flushPut = (>> hFlush stdout) . putStr main :: IO () main = do randomValues <- randomRs (1,100) <$> getStdGen gameLoop Game { values = randomValues, right = 0, rounds = 0 } 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 charactersOriginal file line number Diff line number Diff line change @@ -12,13 +12,13 @@ gameLoop (x:y:values) right rounds = do when keepPlaying $ do let (solution, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] correct <- (solution ==) <$> readLn let (total, message) = if correct then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) putStrLn $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1)] gameLoop values total (rounds + 1) where flushPut = (>> hFlush stdout) . putStr 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ -- Made state passing implicit with StateT import Control.Applicative import Control.Monad import Control.Monad.State @@ -26,12 +26,14 @@ gameLoop = do flushPut $ unwords ["What is", show x, opStr, show y, "? "] correct <- (solution ==) <$> liftIO readLn modify (updateGame correct) gameState' <- get liftIO . putStrLn $ if correct then "Correct!" else unwords ["Sorry! the correct answer is:", show solution] liftIO . putStrLn $ unwords ["You have solved", show $ right gameState', "out of", show $ rounds gameState'] gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ -- Removed syntactic sugar around monad operations. import Control.Applicative import Control.Monad import Control.Monad.State import Data.Char import System.IO import System.Random data Game = Game { values :: [Int], right :: Int, rounds :: Int } updateGame :: Bool -> Game -> Game updateGame correct Game { values = (_:_:remaining) , right = score , rounds = total } = Game { values = remaining , rounds = total + 1 , right = if correct then score + 1 else score } -- >> :: StateT Game IO a -> StateT Game IO b -> StateT Game IO b -- >>= :: StateT Game IO a -> (a -> StateT Game IO b) -> StateT Game IO b -- liftIO :: IO a -> StateT Game IO a -- liftIO . putStrLn :: String -> StateT Game IO () gameLoop :: StateT Game IO () gameLoop = flushPut "Would you like to play? y/n: " >> ("y" ==) . map toLower <$> liftIO getLine >>= \keepPlaying -> when keepPlaying $ gets values >>= \(x:y:_) -> let (solution, opStr) = [(x + y, "+"), (x - y, "-")] !! (x `mod` 2) in flushPut (unwords ["What is", show x, opStr, show y, "? "]) >> (solution ==) <$> liftIO readLn >>= \correct -> modify (updateGame correct) >> get >>= \game -> (liftIO . putStrLn) (unwords [message solution correct, "\nYou have solved", show $ right game, "out of", show $ rounds game]) >> gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr message _ True = "Correct!" message solution _ = unwords ["Sorry! the correct answer is:", show solution] main :: IO () main = randomRs (1,100) <$> getStdGen >>= \randomValues -> evalStateT gameLoop (Game randomValues 0 0) 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ -- Factored message into a function, collapsed modification and state update -- using >> import Control.Applicative import Control.Monad import Control.Monad.State 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 charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ main = getStdGen >>= gameLoop 0 0 . randomRs (1, 100) where let (soln, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) putStr (unwords ["What is", show x, opStr, show y, "? "]) >> hFlush stdout (total, message) <- ap ((.) . updateGame right) (==) soln <$> readLn putStrLn $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1)] gameLoop total (rounds + 1) values) updateGame total _ True = (total + 1, "Correct!") updateGame total solution _ = (total, unwords ["Sorry! the correct answer is:", show solution]) -
cleichner revised this gist
Dec 3, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ -- Used the `Safe` library to be more robust when given invalid input {-# LANGUAGE TemplateHaskell #-} import Control.Applicative import Control.Lens hiding (op) -
cleichner revised this gist
Dec 3, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -32,7 +32,7 @@ gameLoop = do when keepPlaying $ do (x:y:_) <- values <<%= drop 2 numRounds <- rounds <+= 1 let (solution, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) correct <- liftM (solution ==) (getUserInput $ unwords ["What is", show x, opStr, show y ++ "? "]) -
cleichner revised this gist
Dec 3, 2013 . 8 changed files with 10 additions and 18 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -9,10 +9,9 @@ gameLoop (x:y:values) right rounds = do flushPut "Would you like to play? y/n: " keepPlaying <- getLine when (map toLower keepPlaying == "y") $ do let (solution, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] response <- readLn let (total, message) = if solution == response then (right + 1, "Correct!") 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 charactersOriginal file line number Diff line number Diff line change @@ -10,11 +10,10 @@ gameLoop (x:y:values) right rounds = do flushPut "Would you like to play? y/n: " keepPlaying <- ("y" ==) . map toLower <$> getLine when keepPlaying $ do let (solution, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] correct <- (solution ==) <$> readLn let (total, message) = if correct then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) 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 charactersOriginal file line number Diff line number Diff line change @@ -22,10 +22,9 @@ gameLoop = do keepPlaying <- ("y" ==) . map toLower <$> liftIO getLine when keepPlaying $ do (x:y:_) <- gets values let (solution, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] correct <- (solution ==) <$> liftIO readLn game <- modify (updateGame correct) >> get liftIO . putStrLn $ if correct 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 charactersOriginal file line number Diff line number Diff line change @@ -22,10 +22,9 @@ gameLoop = do keepPlaying <- ("y" ==) . map toLower <$> liftIO getLine when keepPlaying $ do (x:y:_) <- gets values let (solution, opStr) = [(x + y, "+"), (x - y, "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] correct <- (solution ==) <$> liftIO readLn game <- modify (updateGame correct) >> get liftIO . putStrLn $ unwords [message solution correct, 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 charactersOriginal file line number Diff line number Diff line change @@ -23,10 +23,9 @@ gameLoop = do keepPlaying <- ("y" ==) . map toLower <$> liftIO getLine when keepPlaying $ do (x:y:_) <- use values let (solution, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] correct <- (solution ==) <$> liftIO readLn game <- modify (updateGame correct) >> get liftIO . putStrLn $ unwords [message solution correct, 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 charactersOriginal file line number Diff line number Diff line change @@ -19,10 +19,8 @@ gameLoop = do (x:y:_) <- values <<%= drop 2 numRounds <- rounds <+= 1 let (solution, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] correct <- (solution ==) <$> liftIO readLn numRight <- right <+= if correct then 1 else 0 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 charactersOriginal file line number Diff line number Diff line change @@ -25,10 +25,9 @@ gameLoop = do (x:y:_) <- values <<%= drop 2 numRounds <- rounds <+= 1 let (solution, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] correctSolution <- (solution ==) <$> liftIO readLn past <- history <%= (History x y opStr correctSolution :) numRight <- right <+= if correctSolution then 1 else 0 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 charactersOriginal file line number Diff line number Diff line change @@ -10,9 +10,9 @@ main = getStdGen >>= gameLoop 0 0 . randomRs (1, 100) where gameLoop right rounds (x:y:values) = do putStr "Would you like to play? y/n: " >> hFlush stdout ("y" ==) . map toLower <$> getLine >>= flip when (do let (soln, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) putStr (unwords ["What is", show x, opStr, show y, "? "]) >> hFlush stdout (total, message) <- ap ((.) . updateGame right) (==) soln <$> readLn putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop total (rounds + 1) values) updateGame total _ True = (total + 1, "Correct!") -
cleichner revised this gist
Dec 2, 2013 . 1 changed file with 52 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ -- Integrated updateGame into gameLoop using state update operators. {-# LANGUAGE TemplateHaskell #-} import Control.Applicative import Control.Lens hiding (op) import Control.Monad import Control.Monad.State import Data.Char import Safe import System.IO import System.Random data Game = Game { _values :: [Int], _right :: Int, _rounds :: Int } makeLenses ''Game flushPut :: String -> StateT Game IO () flushPut = liftIO . (>> hFlush stdout) . putStr getUserInput :: String -> StateT Game IO Int getUserInput prompt = do flushPut prompt line <- liftIO getLine case readMay line of Just value -> return value Nothing -> do liftIO . putStrLn $ unwords ["Error:", show line, "is not a valid number"] getUserInput prompt gameLoop :: StateT Game IO () gameLoop = do flushPut "Would you like to play? y/n: " keepPlaying <- ("y" ==) . map toLower <$> liftIO getLine when keepPlaying $ do (x:y:_) <- values <<%= drop 2 numRounds <- rounds <+= 1 let (solution, opStr) = [(x + y, "+") , (x - y, "-")] !! (x `mod` 2) correct <- liftM (solution ==) (getUserInput $ unwords ["What is", show x, opStr, show y ++ "? "]) numRight <- right <+= if correct then 1 else 0 liftIO . putStrLn $ unwords [message solution correct, "\nYou have solved", show numRight, "out of", show numRounds] gameLoop where message _ True = "Correct!" message soln _ = unwords ["Sorry! the correct answer is:", show soln] main :: IO () main = do randomValues <- randomRs (1,100) <$> getStdGen evalStateT gameLoop (Game randomValues 0 0) -
cleichner revised this gist
Nov 21, 2013 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ -- Added historical data for every problem asked to show flexibility of -- StateT with lens. {-# LANGUAGE TemplateHaskell #-} import Control.Applicative import Control.Lens hiding (op) -
cleichner revised this gist
Nov 21, 2013 . 2 changed files with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ gameLoop = do let solution = x `op` y correct <- (solution ==) <$> liftIO readLn game <- modify (updateGame correct) >> get liftIO . putStrLn $ if correct then "Correct!" else unwords ["Sorry! the correct answer is:", show solution] liftIO . putStr $ unwords 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 charactersOriginal file line number Diff line number Diff line change @@ -29,7 +29,7 @@ gameLoop = do let solution = x `op` y correctSolution <- (solution ==) <$> liftIO readLn past <- history <%= (History x y opStr correctSolution :) numRight <- right <+= if correctSolution then 1 else 0 liftIO . putStrLn $ unwords [message solution correctSolution, -
cleichner revised this gist
Nov 21, 2013 . 2 changed files with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ gameLoop (x:y:values) right rounds = do flushPut $ unwords ["What is", show x, opStr, show y, "? "] let solution = x `op` y correct <- (solution ==) <$> readLn let (total, message) = if correct then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) 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 charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ gameLoop = do flushPut $ unwords ["What is", show x, opStr, show y, "? "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn game <- modify (updateGame correct) >> get liftIO . putStrLn $ if solution == response then "Correct!" -
cleichner revised this gist
Nov 21, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ gameLoop = do flushPut $ unwords ["What is", show x, opStr, show y, "? "] let solution = x `op` y correct <- (solution ==) <$> readLn game <- modify (updateGame correct) >> get liftIO . putStrLn $ if solution == response then "Correct!" -
cleichner revised this gist
Nov 20, 2013 . 2 changed files with 52 additions and 53 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,47 @@ -- Added historical data for every problem asked. {-# LANGUAGE TemplateHaskell #-} import Control.Applicative import Control.Lens hiding (op) import Control.Monad import Control.Monad.State import Data.Char import System.IO import System.Random data History = History { _lhs :: Int, _rhs :: Int, _operator :: String, _correct :: Bool} deriving (Show) makeLenses ''History data Game = Game { _values :: [Int], _right :: Int, _rounds :: Int, _history :: [History] } makeLenses ''Game gameLoop :: StateT Game IO () gameLoop = do flushPut "Would you like to play? y/n: " keepPlaying <- ("y" ==) . map toLower <$> liftIO getLine when keepPlaying $ do (x:y:_) <- values <<%= drop 2 numRounds <- rounds <+= 1 let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] let solution = x `op` y correctSolution <- (solution ==) <$> liftIO readLn past <- history <%= (History x y opStr correctSolution :) numRight <- right <+= if correctSolution then 1 else 0 liftIO . putStrLn $ unwords [message solution correctSolution, "\nYou have solved", show numRight, "out of", show numRounds, show (past ^.. traverse.correct)] gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr message _ True = "Correct!" message soln _ = unwords ["Sorry! the correct answer is:", show soln] main :: IO () main = do randomValues <- randomRs (1,100) <$> getStdGen evalStateT gameLoop (Game randomValues 0 0 []) 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 charactersOriginal file line number Diff line number Diff line change @@ -1,48 +1,19 @@ -- Went golfing. Applied all of the above refactorings, but only to shrink code. import Control.Applicative import Control.Monad import Data.Char import System.IO import System.Random main :: IO () main = getStdGen >>= gameLoop 0 0 . randomRs (1, 100) where gameLoop right rounds (x:y:values) = do putStr "Would you like to play? y/n: " >> hFlush stdout ("y" ==) . map toLower <$> getLine >>= flip when (do let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) putStr (unwords ["What is", show x, opStr, show y, "? "]) >> hFlush stdout (total, message) <- ap ((.) . updateGame right) (==) (x `op` y) <$> readLn putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop total (rounds + 1) values) updateGame total _ True = (total + 1, "Correct!") updateGame total solution _ = (total, unwords ["Sorry! the correct answer is:", show solution]) -
cleichner revised this gist
Nov 20, 2013 . 2 changed files with 49 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -41,4 +41,4 @@ public static boolean keepPlaying(Scanner keyboard) { public static void printQuestion(int x, char op, int y) { System.out.print("What is " + x + " " + op + " " + y + "? "); } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ -- Added historical data for every problem asked. {-# LANGUAGE TemplateHaskell #-} import Control.Applicative import Control.Lens hiding (op) import Control.Monad import Control.Monad.State import Data.Char import System.IO import System.Random data History = History { _lhs :: Int, _rhs :: Int, _operator :: String, _correct :: Bool} deriving (Show) makeLenses ''History data Game = Game { _values :: [Int], _right :: Int, _rounds :: Int, _history :: [History] } makeLenses ''Game gameLoop :: StateT Game IO () gameLoop = do flushPut "Would you like to play? y/n: " keepPlaying <- ("y" ==) . map toLower <$> liftIO getLine when keepPlaying $ do (x:y:_) <- values <<%= drop 2 numRounds <- rounds <+= 1 let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] let solution = x `op` y correctSolution <- (solution ==) <$> liftIO readLn past <- history <%= (History x y opStr correctSolution :) numRight <- right <+= if correctSolution then 1 else 0 liftIO . putStrLn $ unwords [message solution correctSolution, "\nYou have solved", show numRight, "out of", show numRounds, show (past ^.. traverse.correct)] gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr message _ True = "Correct!" message soln _ = unwords ["Sorry! the correct answer is:", show soln] main :: IO () main = do randomValues <- randomRs (1,100) <$> getStdGen evalStateT gameLoop (Game randomValues 0 0 []) -
cleichner revised this gist
Nov 20, 2013 . 7 changed files with 7 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ gameLoop (x:y:values) right rounds = do keepPlaying <- getLine when (map toLower keepPlaying == "y") $ do let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] let solution = x `op` y response <- readLn 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 charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ gameLoop (x:y:values) right rounds = do keepPlaying <- ("y" ==) . map toLower <$> getLine when keepPlaying $ do let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn 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 charactersOriginal file line number Diff line number Diff line change @@ -23,7 +23,7 @@ gameLoop = do when keepPlaying $ do (x:y:_) <- gets values let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn 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 charactersOriginal file line number Diff line number Diff line change @@ -23,7 +23,7 @@ gameLoop = do when keepPlaying $ do (x:y:_) <- gets values let (op, opStr) = [((+),"+"), ((-),"-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn 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 charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,7 @@ gameLoop = do when keepPlaying $ do (x:y:_) <- use values let (op, opStr) = [((+),"+"), ((-),"-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn 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 charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,7 @@ gameLoop = do numRounds <- rounds <+= 1 let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) flushPut $ unwords ["What is", show x, opStr, show y, "? "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn 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 charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ main = getStdGen >>= gameLoop 0 0 . randomRs (1, 100) where putStr "Would you like to play? y/n: " >> hFlush stdout ("y" ==) . map toLower <$> getLine >>= flip when (do let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) putStr (unwords ["What is", show x, opStr, show y, "? "]) >> hFlush stdout (total, message) <- ap ((.) . updateGame right) (==) (x `op` y) <$> readLn putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop total (rounds + 1) values) -
cleichner revised this gist
Nov 20, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ makeLenses ''Game updateGame :: Bool -> Game -> Game updateGame correct = (values %~ drop 2) . (rounds +~ 1) . (right +~ if correct then 1 else 0) -
cleichner revised this gist
Nov 20, 2013 . 2 changed files with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -13,8 +13,8 @@ updateGame correct Game { values = (_:_:remaining) , right = score , rounds = total } = Game { values = remaining , rounds = total + 1 , right = if correct then score + 1 else score } gameLoop :: StateT Game IO () gameLoop = do 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 charactersOriginal file line number Diff line number Diff line change @@ -13,8 +13,8 @@ makeLenses ''Game updateGame :: Bool -> Game -> Game updateGame correct = (values %~ (drop 2)) . (rounds +~ 1) . (right +~ if correct then 1 else 0) gameLoop :: StateT Game IO () -
cleichner revised this gist
Nov 20, 2013 . 4 changed files with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -33,7 +33,7 @@ gameLoop = do gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr message _ True = "Correct!" message solution _ = unwords ["Sorry! the correct answer is:", show solution] main :: IO () 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 charactersOriginal file line number Diff line number Diff line change @@ -34,7 +34,7 @@ gameLoop = do gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr message _ True = "Correct!" message solution _ = unwords ["Sorry! the correct answer is:", show solution] main :: IO () 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 charactersOriginal file line number Diff line number Diff line change @@ -31,7 +31,7 @@ gameLoop = do gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr message _ True = "Correct!" message soln _ = unwords ["Sorry! the correct answer is:", show soln] main :: IO () 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 charactersOriginal file line number Diff line number Diff line change @@ -15,5 +15,5 @@ main = getStdGen >>= gameLoop 0 0 . randomRs (1, 100) where (total, message) <- ap ((.) . updateGame right) (==) (x `op` y) <$> readLn putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop total (rounds + 1) values) updateGame total _ True = (total + 1, "Correct!") updateGame total solution _ = (total, unwords ["Sorry! the correct answer is:", show solution]) -
cleichner revised this gist
Nov 20, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ -- Integrated updateGame into gameLoop using state update operators. {-# LANGUAGE TemplateHaskell #-} import Control.Applicative import Control.Lens hiding (op) -
cleichner revised this gist
Nov 20, 2013 . 4 changed files with 65 additions and 22 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -39,4 +39,4 @@ gameLoop = do main :: IO () main = do randomValues <- randomRs (1,100) <$> getStdGen evalStateT gameLoop (Game randomValues 0 0) 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ -- Introduced Control.Lens {-# LANGUAGE TemplateHaskell #-} import Control.Applicative import Control.Lens hiding (op) @@ -11,28 +11,31 @@ import System.Random data Game = Game { _values :: [Int], _right :: Int, _rounds :: Int } makeLenses ''Game updateGame :: Bool -> Game -> Game updateGame correct = (rounds +~ 1) . (values %~ (drop 2)) . (right +~ if correct then 1 else 0) gameLoop :: StateT Game IO () gameLoop = do flushPut "Would you like to play? y/n: " keepPlaying <- ("y" ==) . map toLower <$> liftIO getLine when keepPlaying $ do (x:y:_) <- use values let (op, opStr) = [((+),"+"), ((-),"-")] !! (x `mod` 2) flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn game <- modify (updateGame correct) >> get liftIO . putStrLn $ unwords [message solution correct, "\nYou have solved", show $ game ^. right, "out of", show $ game ^. rounds] gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr message _ True = "Correct" message solution _ = unwords ["Sorry! the correct answer is:", show solution] main :: IO () main = do 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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,40 @@ -- Introduced Control.Lens and made significant use of state update operators. {-# LANGUAGE TemplateHaskell #-} import Control.Applicative import Control.Lens hiding (op) import Control.Monad import Control.Monad.State import Data.Char import System.IO import System.Random data Game = Game { _values :: [Int], _right :: Int, _rounds :: Int } makeLenses ''Game gameLoop :: StateT Game IO () gameLoop = do flushPut "Would you like to play? y/n: " keepPlaying <- ("y" ==) . map toLower <$> liftIO getLine when keepPlaying $ do (x:y:_) <- values <<%= drop 2 numRounds <- rounds <+= 1 let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn numRight <- right <+= if correct then 1 else 0 liftIO . putStrLn $ unwords [message solution correct, "\nYou have solved", show numRight, "out of", show numRounds] gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr message _ True = "Correct" message soln _ = unwords ["Sorry! the correct answer is:", show soln] main :: IO () main = do randomValues <- randomRs (1,100) <$> getStdGen evalStateT gameLoop (Game randomValues 0 0) 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ -- Went golfing. Applied all of the above refactorings, but only to shrink code. import Control.Applicative import Control.Monad import Data.Char import System.IO import System.Random main :: IO () main = getStdGen >>= gameLoop 0 0 . randomRs (1, 100) where gameLoop right rounds (x:y:values) = do putStr "Would you like to play? y/n: " >> hFlush stdout ("y" ==) . map toLower <$> getLine >>= flip when (do let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) putStr (unwords [show x, opStr, show y, "= "]) >> hFlush stdout (total, message) <- ap ((.) . updateGame right) (==) (x `op` y) <$> readLn putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop total (rounds + 1) values) updateGame total _ True = (total + 1, "Correct") updateGame total solution _ = (total, unwords ["Sorry! the correct answer is:", show solution]) -
cleichner revised this gist
Nov 20, 2013 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,8 @@ gameLoop (x:y:values) right rounds = do putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop values total (rounds + 1) where flushPut = (>> hFlush stdout) . putStr main :: IO () main = do -
cleichner revised this gist
Nov 20, 2013 . 2 changed files with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -14,8 +14,8 @@ gameLoop (x:y:values) right rounds = do flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn let (total, message) = if correct then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) putStr $ unwords 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 charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ gameLoop = do flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn game <- modify (updateGame correct) >> get liftIO . putStrLn $ if solution == response then "Correct!" -
cleichner revised this gist
Nov 20, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ gameLoop (x:y:values) right rounds = do flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y response <- readLn let (total, message) = if solution == response then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) -
cleichner revised this gist
Nov 19, 2013 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -10,8 +10,9 @@ main = getStdGen >>= gameLoop 0 0 . randomRs (1, 100) where gameLoop right rounds (x:y:values) = do putStr "Would you like to play? y/n: " >> hFlush stdout ("y" ==) . map toLower <$> getLine >>= flip when (do let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) putStr (unwords [show x, opStr, show y, "= "]) >> hFlush stdout (total, message) <- ap ((.) . updateGame right) (==) (x `op` y) <$> readLn putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop total (rounds + 1) values) updateGame total _ True = (total + 1, "Correct") -
cleichner revised this gist
Nov 17, 2013 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -10,9 +10,8 @@ main = getStdGen >>= gameLoop 0 0 . randomRs (1, 100) where gameLoop right rounds (x:y:values) = do putStr "Would you like to play? y/n: " >> hFlush stdout ("y" ==) . map toLower <$> getLine >>= flip when (do putStr (unwords [show x, ["+" , "-"] !! (x `mod` 2), show y, "= "]) >> hFlush stdout (total, message) <- ap ((.) . updateGame right) (==) (([(+),(-)] !! (x `mod` 2)) x y) <$> readLn putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop total (rounds + 1) values) updateGame total _ True = (total + 1, "Correct") -
cleichner revised this gist
Nov 16, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,7 @@ gameLoop = do let solution = x `op` y correct <- (solution ==) <$> liftIO readLn numRight <- right <+= if correct then 1 else 0 liftIO . putStrLn $ unwords [message solution correct, "\nYou have solved", show numRight, "out of", show numRounds] -
cleichner revised this gist
Nov 16, 2013 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -17,17 +17,17 @@ gameLoop = do keepPlaying <- ("y" ==) . map toLower <$> liftIO getLine when keepPlaying $ do (x:y:_) <- values <<%= drop 2 numRounds <- rounds <+= 1 let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn numRight <-right <+= if correct then 1 else 0 liftIO . putStrLn $ unwords [message solution correct, "\nYou have solved", show numRight, "out of", show numRounds] gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr -
cleichner revised this gist
Nov 16, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ -- Introduced Control.Lens and made significant use of state update operators. {-# LANGUAGE TemplateHaskell #-} import Control.Applicative import Control.Lens hiding (op) import Control.Monad import Control.Monad.State import Data.Char -
cleichner revised this gist
Nov 16, 2013 . 3 changed files with 54 additions and 14 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -28,8 +28,8 @@ gameLoop = do let solution = x `op` y correct <- (solution ==) <$> liftIO readLn game <- modify (updateGame correct) >> get liftIO . putStrLn $ unwords [message solution correct, "\nYou have solved", show $ right game, "out of", show $ rounds game] gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr 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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,40 @@ -- Introduced Control.Lens and made significant use of state update operators. {-# LANGUAGE TemplateHaskell #-} import Control.Lens hiding (op) import Control.Applicative import Control.Monad import Control.Monad.State import Data.Char import System.IO import System.Random data Game = Game { _values :: [Int], _right :: Int, _rounds :: Int } makeLenses ''Game gameLoop :: StateT Game IO () gameLoop = do flushPut "Would you like to play? y/n: " keepPlaying <- ("y" ==) . map toLower <$> liftIO getLine when keepPlaying $ do (x:y:_) <- values <<%= drop 2 numRounds <- show <$> (rounds <+= 1) let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn numRight <- show <$> (right <+= if correct then 1 else 0) liftIO . putStrLn $ unwords [message solution correct, "\nYou have solved", numRight, "out of", numRounds] gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr message _ True = "Correct" message soln _ = unwords ["Sorry! the correct answer is:", show soln] main :: IO () main = do randomValues <- randomRs (1,100) <$> getStdGen evalStateT gameLoop (Game randomValues 0 0) 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ -- Went golfing. Applied all of the above refactorings, but only to shrink code. import Control.Applicative import Control.Monad import Data.Char import System.IO import System.Random main :: IO () main = getStdGen >>= gameLoop 0 0 . randomRs (1, 100) where gameLoop right rounds (x:y:values) = do putStr "Would you like to play? y/n: " >> hFlush stdout ("y" ==) . map toLower <$> getLine >>= flip when (do let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) putStr (unwords [show x, opStr, show y, "= "]) >> hFlush stdout (total, message) <- ap ((.) . updateGame right) (==) (x `op` y) <$> readLn putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop total (rounds + 1) values) updateGame total _ True = (total + 1, "Correct") updateGame total solution _ = (total, unwords ["Sorry! the correct answer is:", show solution]) -
cleichner revised this gist
Jul 28, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -25,5 +25,5 @@ gameLoop (x:y:values) right rounds = do main :: IO () main = do randomValues <- randomRs (1,100) <$> getStdGen gameLoop randomValues 0 0 -
cleichner revised this gist
Jul 28, 2013 . 3 changed files with 5 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ gameLoop (x:y:values) right rounds = do let solution = x `op` y response <- readLn let (total, message) = if solution == response then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) putStr $ unwords 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 charactersOriginal file line number Diff line number Diff line change @@ -14,8 +14,8 @@ gameLoop (x:y:values) right rounds = do flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- readLn let (total, message) = if solution == response then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) putStr $ unwords 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 charactersOriginal file line number Diff line number Diff line change @@ -26,9 +26,9 @@ gameLoop = do flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y response <- liftIO readLn game <- modify (updateGame correct) >> get liftIO . putStrLn $ if solution == response then "Correct!" else unwords ["Sorry! the correct answer is:", show solution] liftIO . putStr $ unwords -
cleichner revised this gist
Jul 26, 2013 . 5 changed files with 8 additions and 8 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -13,8 +13,8 @@ gameLoop (x:y:values) right rounds = do flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y response <- readLn let (total, message) = if response == solution then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) putStr $ unwords 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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ gameLoop (x:y:values) right rounds = do flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- (solution ==) <$> readLn let (total, message) = if correct then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) 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 charactersOriginal file line number Diff line number Diff line change @@ -26,12 +26,12 @@ gameLoop = do flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn game <- modify (updateGame correct) >> get liftIO . putStrLn $ if correct then "Correct!" else unwords ["Sorry! the correct answer is:", show solution] liftIO . putStr $ unwords ["You have solved", show $ right game, "out of", show $ rounds game, "\n"] gameLoop where 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 charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ gameLoop = do flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- (solution ==) <$> liftIO readLn game <- modify (updateGame correct) >> get liftIO . putStr $ unwords [message solution correct, "\nYou have solved", show $ right game, "out of", show $ rounds game, "\n"] 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 charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,7 @@ main = getStdGen >>= gameLoop 0 0 . randomRs (1, 100) where ("y" ==) . map toLower <$> getLine >>= flip when (do let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) putStr (unwords [show x, opStr, show y, "= "]) >> hFlush stdout (total, message) <- ap ((.) . updateGame right) (==) (x `op` y) <$> readLn putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop total (rounds + 1) values) updateGame total _ True = (total + 1, "Correct") -
cleichner created this gist
Jul 26, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ // Java code that started it all import java.util.Random; import java.util.Scanner; public class RandomProblem { public static void main(String[] args) { int right = 0; int rounds = 0; Scanner keyboard = new Scanner(System.in); Random rand = new Random(); while (keepPlaying(keyboard)) { int x = rand.nextInt(100) + 1; int y = rand.nextInt(100) + 1; int solution = 0; if (rand.nextBoolean()) { solution = x + y; printQuestion(x, '+', y); } else { solution = x - y; printQuestion(x, '-', y); } rounds++; if (solution == keyboard.nextInt()) { System.out.println("Correct!"); right++; } else { System.out.println("Sorry! the correct answer is: " + solution); } System.out.println("You have solved " + right + " out of " + rounds + " problems correctly."); } } public static boolean keepPlaying(Scanner keyboard) { System.out.print("Would you like to play? y/n: "); return keyboard.next().toLowerCase().equals("y"); } public static void printQuestion(int x, char op, int y) { System.out.print("What is " + x + " " + op + " " + y + "? "); } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ -- First Haskell version, nothing tricky import Control.Monad import Data.Char import System.IO import System.Random gameLoop :: [Int] -> Int -> Int -> IO () gameLoop (x:y:values) right rounds = do flushPut "Would you like to play? y/n: " keepPlaying <- getLine when (map toLower keepPlaying == "y") $ do let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y response <- getLine let (total, message) = if read response == solution then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop values total (rounds + 1) where flushPut s = putStr s >> hFlush stdout main :: IO () main = do gen <- getStdGen gameLoop (randomRs (1, 100) gen) 0 0 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ -- Introduced applicative and pointfree style import Control.Applicative import Control.Monad import Data.Char import System.IO import System.Random gameLoop :: [Int] -> Int -> Int -> IO () gameLoop (x:y:values) right rounds = do flushPut "Would you like to play? y/n: " keepPlaying <- ("y" ==) . map toLower <$> getLine when keepPlaying $ do let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- (solution ==) . read <$> getLine let (total, message) = if correct then (right + 1, "Correct!") else (right, unwords ["Sorry! the correct answer is:", show solution]) putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop values total (rounds + 1) where flushPut = (>> hFlush stdout) . putStr main :: IO () main = do gen <- getStdGen gameLoop (randomRs (1,100) gen) 0 0 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ -- Made state-passing explicit with StateT and record import Control.Applicative import Control.Monad import Control.Monad.State import Data.Char import System.IO import System.Random data Game = Game { values :: [Int], right :: Int, rounds :: Int } updateGame :: Bool -> Game -> Game updateGame correct Game { values = (_:_:remaining) , right = score , rounds = total } = Game { values = remaining , right = if correct then score + 1 else score , rounds = total + 1 } gameLoop :: StateT Game IO () gameLoop = do flushPut "Would you like to play? y/n: " keepPlaying <- ("y" ==) . map toLower <$> liftIO getLine when keepPlaying $ do (x:y:_) <- gets values let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- (solution ==) . read <$> liftIO getLine game <- modify (updateGame correct) >> get liftIO . putStrLn $ unwords if correct then "Correct!" else unwords ["Sorry! the correct answer is:", show solution] liftIO . putStr $ ["You have solved", show $ right game, "out of", show $ rounds game, "\n"] gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr main :: IO () main = do randomValues <- randomRs (1,100) <$> getStdGen evalStateT gameLoop Game { values = randomValues, right = 0, rounds = 0 } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ -- Factored message into a function import Control.Applicative import Control.Monad import Control.Monad.State import Data.Char import System.IO import System.Random data Game = Game { values :: [Int], right :: Int, rounds :: Int } updateGame :: Bool -> Game -> Game updateGame correct Game { values = (_:_:remaining) , right = score , rounds = total } = Game { values = remaining , right = if correct then score + 1 else score , rounds = total + 1 } gameLoop :: StateT Game IO () gameLoop = do flushPut "Would you like to play? y/n: " keepPlaying <- ("y" ==) . map toLower <$> liftIO getLine when keepPlaying $ do (x:y:_) <- gets values let (op, opStr) = [((+),"+"), ((-),"-")] !! (x `mod` 2) flushPut $ unwords [show x, opStr, show y, "= "] let solution = x `op` y correct <- (solution ==) . read <$> liftIO getLine game <- modify (updateGame correct) >> get liftIO . putStr $ unwords [message solution correct, "\nYou have solved", show $ right game, "out of", show $ rounds game, "\n"] gameLoop where flushPut = liftIO . (>> hFlush stdout) . putStr message _ True = "Correct" message solution _ = unwords ["Sorry! the correct answer is:", show solution] main :: IO () main = do randomValues <- randomRs (1,100) <$> getStdGen evalStateT gameLoop Game { values = randomValues, right = 0, rounds = 0 } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ -- Went golfing. Applied all of the above refactorings, but only to shrink code. import Control.Applicative import Control.Monad import Data.Char import System.IO import System.Random main :: IO () main = getStdGen >>= gameLoop 0 0 . randomRs (1, 100) where gameLoop right rounds (x:y:values) = do putStr "Would you like to play? y/n: " >> hFlush stdout ("y" ==) . map toLower <$> getLine >>= flip when (do let (op, opStr) = [((+), "+") , ((-), "-")] !! (x `mod` 2) putStr (unwords [show x, opStr, show y, "= "]) >> hFlush stdout (total, message) <- ap ((.) . updateGame right) ((. read) . (==)) (x `op` y) <$> getLine putStr $ unwords [message, "\nYou have solved", show total, "out of", show (rounds + 1), "\n"] gameLoop total (rounds + 1) values) updateGame total _ True = (total + 1, "Correct") updateGame total solution _ = (total, unwords ["Sorry! the correct answer is:", show solution])