import System.IO
makeGrid :: Int -> Int -> a -> [[a]]
makeGrid x y = replicate y . replicate x
startGame = do
putStrLn "Select a difficulty (1,2): "
difficulty <- getLine
|difficulty == '1' = makeGrid 3 3 False
|difficulty == '2' = makeGrid 5 5 False
|otherwise = putStrln "Wrong thing"
some function --start from beginning again
如你所见,我有一个函数makeGrid。我想在startGame中获取用户输入并根据用户输入调用makeGrid。如果可能的话,还要做一个while循环,我该怎么做?
答案 0 :(得分:3)
{-# LANGUAGE LambdaCase #-}
import System.IO
playGame = do
grid <- initializeGrid
-- game code goes here
initializeGrid = do
putStrLn "Select a difficulty (1,2): "
getLine >>= \case -- "difficulty <- getLine" <newline> "case difficulty of" also ok
"1" -> return $ makeGrid 3 3 False
"2" -> return $ makeGrid 5 5 False
_ -> do
putStrln "Wrong thing"
initializeGrid
makeGrid :: Int -> Int -> a -> [[a]]
makeGrid x y = replicate y . replicate x