1

I find it a little difficult to understand the Haskell world, so I would really appreciate some help!

I want to create 2 dimensional array of Chars (a matrix 10x10 of symbols) and it should be mutable, so I tried this:

import Data.Array.IO
arr <- newArray ((1,10), (1,10)) '!' :: IO (IOArray (Int, Int) Char)
a <- readArray arr (1,1)

but it didn't worked out. Could you please tell me how to create the array and how to access its members, or if this isn't a good way, another way to do this?

And also I would like to ask if there is a way to color some of the elements in the array in a different color. I imported System.Console.ANSI but I'm not quite sure how exactly to color what I want to be in a different color. A example of outputting a red letter would be really helpfull.

Thank you very much in advance! :)

4
  • 1
    Two things: First, why do you want a mutable array? Chances are you actually don't. Second, coloring "elements of the array" makes no sense as such. That's an output issue, and a completely separate question. Commented Jan 4, 2013 at 17:35
  • I'm not sure if it has to be mutable, I would like to try to implement game of life and the array will be changing on each iteration, so I thought that it's better to be mutable. As for the coloring, I wanted the cells which are dead and alive to be in different colors. I know that it is another topic, but I would feel a bit guilty for posting two haskell questions at the same time. Commented Jan 4, 2013 at 18:28
  • You're more likely to get helpful answers if your question has a single purpose and clearly explains the problem you're trying to solve. I'd suggest clarifying your overall goal (instead of assuming a particular kind of solution) and posting a separate question about colored output, possibly after getting the rest of the program working if you're worried about posting too many questions at once. Commented Jan 4, 2013 at 18:39
  • For a "Game of Life", if memory serves, an array is not the best choice (and that comes from someone who loves arrays, in particular mutable arrays [the ST kind, not the IO kind, though]). There are typically so many free cells that scanning them is much more expensive than a lookup in e.g. a Map. Commented Jan 4, 2013 at 20:08

1 Answer 1

1

This compiles and runs without error on my laptop.

import Data.Array.IO       -- from the array package
import System.Console.ANSI -- from the ansi-terminal package

main :: IO ()
main = do
        arr <- newArray ((1,1), (10,10)) '!' :: IO (IOArray (Int, Int) Char)
        -- You had ((1,10), (1,10)), in the line above.
        -- That meant (1,10) was the only valid index!
        ch <- readArray arr (1,1)
        setSGR [SetColor Foreground Dull Red]   -- set foreground colour to red
        putStr [ch]
        setSGR []                               -- reset colours
        putStrLn ""

I second C. A. McCann's suggestion of using normal, immutable arrays instead of mutable arrays. (Unfortunately I have little experience of using arrays in Haskell, so I'll leave it to someone else to suggest how to compute an updated array from the old array in a single step.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.