<div dir="ltr"><font face="monospace, monospace">data Piece = X | O</font><div><br></div><div>means that X and O are constants, having data type 'Piece'.</div><div>In other words, you directly use X and O instead of Piece X and Piece O.</div><div><br></div><div>Piece is a type, and X and O are constructors (or simply the two possible values).</div><div><br></div><div>A good similar example is the Bool type,</div><div><br></div><div><font face="monospace, monospace"> data Bool = False | True</font></div><div><font face="monospace, monospace"> -- Two possible values, i.e. False and True</font></div><div><br></div><div>For more info, take a look here: <a href="http://en.wikibooks.org/wiki/Haskell/Type_declarations">http://en.wikibooks.org/wiki/Haskell/Type_declarations</a></div><div><br></div><div>Also, to make life easier, you might want to use:</div><div><br></div><div><font face="monospace, monospace"> data Piece = X | O</font></div><div><font face="monospace, monospace"> deriving Show -- Make this type showable</font></div><div><font face="monospace, monospace"><br></font></div>With this, you will be able to use 'print' with elements of this datatype.<div><br></div><div>For a tutorial on setting up emacs, take a look here: <a href="https://github.com/serras/emacs-haskell-tutorial/blob/master/tutorial.md">https://github.com/serras/emacs-haskell-tutorial/blob/master/tutorial.md</a></div><div><br></div><div>For updating nested lists, you have to create a function that takes two numbers (the positions) and iterates over the whole structure, just updating the required position.</div><div>While this may seem like an overkill, it gets optimized by ghc.</div><div><br></div><div>I don't have any experience with lenses, but they should be usable here. Understanding them will require a good understanding of the type system.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 13 March 2015 at 04:32, Timothy Washington <span dir="ltr"><<a href="mailto:twashing@gmail.com" target="_blank">twashing@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">To get started, I'm trying to implement a simple <i>tictactoe</i> game. And I would like to be able to represent a Piece on the board, as either the string "X" or "O". This is what I have so far. <div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>module Main where</div></div><div><div><br></div></div><div><div>data Piece = X | O</div></div><div><div>type Row = [Piece]</div></div><div><div>type Board = [Row]</div></div><div><div><br></div></div><div><div>-- put an X or O in a position</div></div><div><div>move :: Board -> Piece -> Board</div></div><div><div>move board piece = board</div></div><div><div> </div></div><div><div>-- check win vertically</div></div><div><div>-- check win horizontally</div></div><div><div>-- check win diagonally</div></div><div><div><br></div></div><div><div>main :: IO ()</div></div><div><div>main = putStrLn "Hello World"</div></div></blockquote><div><br></div><div><br></div><div><b><i>A)</i></b> Now, I'd like to be able to <b><i>load code interactively</i></b>, preferably within emacs. However I don't have access to my types with <b><i>ghci</i></b> or <b><i>ghc-mod (Interactive-Haskell)</i></b>. In either case, this call fails with the below error.</div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><br></div></div><div><div>let p = Piece X</div></div><div><div><font color="#990000"><interactive>:20:9-13: Not in scope: data constructor `Piece'</font></div></div></blockquote><div><div><br></div><div><b><i>B)</i></b> And how do I make a <b><i>custom datatype</i></b> that's one of two strings (enumeration of either "X" or "O"). Cabal builds and runs the abouve code, so I know it can compile. But I'm confused as to where X or O is defined, and how I would supply it as an input. </div><div><br></div><div><b><i>C)</i></b> Finally, how do we update nested lists in Haskell. I want the move function to take a Board, Piece, and Position, and return a Board. I see some <a href="https://www.haskell.org/hoogle/?hoogle=update" target="_blank">results from Hoogle</a>. Is this where <a href="https://www.haskell.org/hoogle/?hoogle=lens" target="_blank">Lenses</a> or <a href="https://www.haskell.org/hoogle/?hoogle=zipper" target="_blank">Zippers</a> come into play?</div><div><br></div><div><br></div><div>Thanks </div><div><br></div><div><div><div dir="ltr"><div style="font-family:arial;font-size:small">Tim Washington </div><div style="font-family:arial;font-size:small"><a href="http://interruptsoftware.com" style="color:rgb(17,85,204)" target="_blank">Interruptsoftware.com</a> </div><div style="font-family:arial;font-size:small"><br></div></div></div></div>
</div></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Regards</div><div dir="ltr"><div><br></div><div>Sumit Sahrawat</div></div></div></div></div></div></div>
</div>