-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
45 lines (37 loc) · 1.18 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- option 1 (https://github.com/PiotrJustyna/haskell-anywhere):
-- ./ghci.bat C:\Users\piotr_justyna\Documents\github\programming-in-haskell\part1_chapter10_exercise2
-- option 2 (stack):
-- stack ghci
-- option 3 (ghci):
-- ghci
--
-- :load Main
main = do
putStrLn "initial:"
putStrLn . show $ initial
putStrLn "putBoard initial:"
putBoard initial
putStrLn "putBoard' initial:"
putBoard' initial
initial :: Board
initial = [5, 4, 3, 2, 1]
type Board = [Int]
putRow :: Int -> Int -> IO ()
putRow row numberOfStars = do
putStr (show row)
putStr ": "
putStrLn (concat (replicate numberOfStars "* "))
putBoard :: Board -> IO ()
putBoard board = sequence_ [putRow index value | (index, value) <- zip [1 ..] board]
putBoard' :: Board -> IO ()
putBoard' board = putBoardHelper board 1
putBoardHelper :: Board -> Int -> IO ()
putBoardHelper board currentRowNumber = do
putRow currentRowNumber (board !! currentRowIndex)
if currentRowNumber < (length board)
then
putBoardHelper board (currentRowNumber + 1)
else
return ()
where
currentRowIndex = currentRowNumber - 1