I am trying to make Conway's Game of Life in Haskell. I am very new to Haskell and functional programming in general, and having trouble making an array in haskell to represent the game board.
I am trying to initiate a random array to begin the game. To do that I create a function randomBoard which returns '*' or ' ' to represent a space on the game board.
I want to be able to create the game board array with this function. I havent been able to succesfully instantiate an array yet. I am hoping there is a way i can declare an array of say size 100 and use my random function to set each element.
import System.Random
import Data.Array
import Data.List
randomBoard =
do
f1 <- randomIO :: IO Int
if(f1 `mod` 2) == 0
then return '*'
else return ' '
boardArray :: Array Int Char
boardArray = listArray (0, 100) . map randomBoard $ 100 (0,100)
This obviously doesnt work or even compile. I am sure there are a couple things wrong with it, as I am not sure really how to even work with IO in haskell and produce this outcome. Any guidance is much appreciated...
Data.Arrayand just use a nested[[Bool]]for the game board.Data.Arrayis much more efficient, but a nested list is much easier to use especially for beginners. Additionally it is easier to use aBoolas the state type rather than aChar, as aBoolnaturally maps to the on/off nature of the cell state.