Now there is a table printed, but formatting is ~~

main
Christina 8 months ago
parent 6f6e12c328
commit ed7ba08c54
Signed by: cafkafk
GPG Key ID: 8ED4DEF7375A30C8
  1. 104
      app/Main.hs
  2. 3
      mathdh.cabal
  3. 1
      package.yaml

@ -1,3 +1,6 @@
-- {-# LANGUAGE BangPatterns #-}
-- -- https://blog.ocharles.org.uk/posts/2014-12-05-bang-patterns.html
-- Mathdh - MDH - Math Drills with Haskell
-- Copyright (C) 2021 Christina (cafkafk) - CESC
@ -37,71 +40,112 @@
module Main where
import Lib
import Control.Monad (forM_) -- I am so sorry
import Control.Exception
import Data.Maybe
import Data.List
import System.IO
import System.Random
import System.Environment
import System.Process
import Lib
import Control.Exception
import Control.Monad ( forM_ ) -- I am so sorry
import Data.Array
import Data.List
import Data.Maybe
import System.Environment
import System.IO
import System.Process
import System.Random
import Text.Printf
import Text.Read
-- TODO Perhaps later
-- -- https://stackoverflow.com/questions/5929377/format-list-output-in-haskell
-- import Text.PrettyPrint
--
-- data PrettyTable = PrettyTable { }
-- printGrid :: Show a => Array (Int, Int) a -> IO [()]
-- printGrid grid =
-- sequence $ map (putStrLn . textRepresentation) $ toSimpleArray grid
--
-- toSimpleArray :: Array (Int, Int) a -> [[a]]
-- toSimpleArray grid =
-- [ [ grid ! (x, y) | x <- [lowx .. highx] ] | y <- [lowy .. highy] ]
-- where ((lowx, lowy), (highx, highy)) = bounds grid
--
-- textRepresentation :: Show a => [a] -> String
-- textRepresentation row = foldl (\acc y -> acc ++ (show y) ++ " ") "" row
-- printGrid :: Show a => Array (Int, Int) a -> IO [()]
-- printGrid grid =
-- sequence $ map (putStrLn . textRepresentation) $ toSimpleArray grid
--
-- toSimpleArray :: Array (Int, Int) a -> [[a]]
-- toSimpleArray grid =
-- [ [ grid ! (x, y) | x <- [lowx .. highx] ] | y <- [lowy .. highy] ]
-- where ((lowx, lowy), (highx, highy)) = bounds grid
--
-- textRepresentation :: Show a => [a] -> String
-- textRepresentation row = foldl (\acc y -> acc ++ (show y) ++ " ") "" row
import Text.Read
import Text.Printf
-- Do IO n times
play n f = forM_ [1..n] $ \_ -> do
play n f = forM_ [1 .. n] $ \_ -> do
f
-- Table generator
-- Generates kayleigh table inputs as 2-tuples forall a,b in [a,b]
table a b = [(x,y) | x <- [a..b], y <- [a..b]]
table :: Int -> Int -> [(Int, Int)]
table a b = [ (x, y) | x <- [a .. b], y <- [a .. b] ]
multTable a b = [ [ x * y | x <- [a .. b] ] | y <- [a .. b] ]
-- prettyTable :: [(Int, Int)] -> [Char]
-- prettyTable t =
-- TODO This could well be a record.
-- Performs operation from string of operator
sapp :: [Char] -> Int -> Int -> Int
sapp "+" a b = a + b
sapp "-" a b = a - b
sapp "+" a b = a + b
sapp "-" a b = a - b
sapp "*" a b = a * b
-- Ensure number is entered
forceInput = do
input <- getLine
if not $ isNothing(readMaybe input :: Maybe Int)
then return input
else forceInput
if not $ isNothing (readMaybe input :: Maybe Int)
then return input
else forceInput
-- Runs the input
game op a b = do
putStrLn $ "What is " ++ show a ++ op ++ show b ++ "?"
input <- forceInput
let guess = (read input :: Int)
input <- forceInput
let guess = (read input :: Int)
let answer = sapp op a b
system "clear"
putStrLn $ if (answer == guess) then "Correct" else "Wrong, it was " ++ show answer
putStrLn
$ if (answer == guess) then "Correct" else "Wrong, it was " ++ show answer
-- Deligate numbers to ops and runs game
drill n a b op = do
g <- getStdGen
let rTable = [(x,y) |
x <- [a..b],
y <- (take n (randomRs (a, b) g :: [Int]))]
sequence $ map (\(x,y) -> game op x y) (table a b)
sequence $ map (\(x,y) -> game op x y) rTable
let rTable =
[ (x, y) | x <- [a .. b], y <- (take n (randomRs (a, b) g :: [Int])) ]
sequence $ map (\(x, y) -> game op x y) (table a b)
sequence $ map (\(x, y) -> game op x y) rTable
-- "Gameloop"
main :: IO ()
main = do
args <- getArgs
let n = (read (args !! 0) :: Int)
let a = (read (args !! 1) :: Int)
let b = (read (args !! 2) :: Int)
mapM_ (putStrLn) $ map (show) (multTable a b)
let op = drop 3 args
mapM (drill n a b) op

@ -34,6 +34,7 @@ library
base >=4.7 && <5
, process
, random
, array
default-language: Haskell2010
executable mdh
@ -48,6 +49,7 @@ executable mdh
, mathdh
, process
, random
, array
default-language: Haskell2010
test-suite mathdh-test
@ -63,4 +65,5 @@ test-suite mathdh-test
, mathdh
, process
, random
, array
default-language: Haskell2010

@ -23,6 +23,7 @@ dependencies:
- base >= 4.7 && < 5
- random
- process
- array
library:
source-dirs: src

Loading…
Cancel
Save