I was trying to make Tic Tac Toe game for the monthly tutorial and wrote this code to make a box first:
box :: [String]
box = ["0 | 1 | 2",
"---------",
"3 | 4 | 5",
"---------",
"6 | 7 | 8"]
I get this output in GHCi:
["0 | 1 | 2\n","---------\n","3 | 4 | 5\n","---------\n","6 | 7 | 8\n"]
I wanted to get:
0 | 1 | 2
---------
3 | 4 | 5
---------
6 | 7 | 8
How can I print the grid like this?
Try something like:
Then output the box using
putStr:What
unlinesdoes is take a list of strings and join them together using newlines. It basically treats the list of strings as a list of lines and turns them into a single string.putStrjust prints the string to STDOUT. If you usedprintor GHCi to look at the string instead, newlines would be rendered as\nrather than actual newlines. This happens because theshowinstance of aStringis designed to serialize the string as you would have inputted it rather than printing it. Bothprintand GHCi useshowbefore outputting to STDOUT.If you are at the GHCi prompt and want to see what the string actually looks like, you can use
putStrdirectly: