<div dir="ltr"><div><font color="#000000">Hey man... </font></div><div><br></div><div><font color="#999999"><br></font></div><div class="gmail_extra"><div class="gmail_quote"><font color="#999999">On Mon, Mar 23, 2015 at 4:57 AM, Bob Hutchison <span dir="ltr"><<a href="mailto:hutch-lists@recursive.ca" target="_blank">hutch-lists@recursive.ca</a>></span> wrote:<br></font><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><font color="#999999">Hi Tim,</font><div><font color="#999999"><br></font></div><div><font color="#999999">Straying from clojure? :-)</font></div><div><font color="#999999"><br></font></div></div></blockquote><div><font color="#000000"><br></font></div><div><font color="#000000">Lol, love it!! </font><span style="color:rgb(0,0,0)">I'm a free spirit. Can't hold me down :)</span></div><div><br></div><div class="gmail_extra"><div class="gmail_signature"><div dir="ltr"></div></div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><div><div class="h5"><blockquote type="cite"><div><font color="#999999">On Mar 22, 2015, at 12:33 PM, Timothy Washington <<a href="mailto:twashing@gmail.com" target="_blank">twashing@gmail.com</a>> wrote:</font></div><font color="#999999"><br></font><div><div dir="ltr"><div><font color="#999999">So I've finally had a chance to revisit my tictactoe game. </font></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font color="#999999"><br>import Control.Lens</font></div><div><font color="#999999"><br></font></div><div><font color="#999999">data Piece = X | O | E deriving Show</font></div><div><font color="#999999">type Row = [Piece]</font></div><div><font color="#999999">type Board = [Row]</font></div><div><b><i><font color="#999999">data Position = Int Int deriving Show</font></i></b></div><div><font color="#999999"><br></font></div><div><font color="#999999">-- put an X or O in a position</font></div><div><b><i><font color="#999999">move :: Board -> Piece -> Position -> Board</font></i></b></div><div><font color="#999999">move board piece position = board</font></div><div><font color="#999999"><br></font></div><div><font color="#999999">main :: IO ()</font></div><div><font color="#999999">main = putStrLn "Hello World"</font></div><div><br></div></blockquote></div><div><font color="#999999">Now, if I want to make a move, I'll want to set a <b><i>piece</i></b> on a <b><i>position</i></b>, on a <b><i>board</i></b>. Let's assume the board has the following shape. </font></div><div><font color="#999999"><br></font></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><font color="#999999"><span>λ></span><span> </span><font>let r1 = (E,E,E)</font></font></div></div><div><div><font color="#999999"><span>λ></span><span> </span><font>let r2 = (E,E,E)</font></font></div></div><div><div><font color="#999999"><span>λ></span><span> </span><font>let r3 = (E,E,E)</font></font></div></div><div><div><font color="#999999">λ> let board = (r1,r2,r3)</font></div></div><div><font color="#999999">((E,E,E),(E,E,E),(E,E,E))</font></div></blockquote><div><font color="#999999"><br></font></div><div><font color="#999999">To return an updated board, I dug around and <b><i>i)</i></b> couldn't find a core Haskell equivalent to Clojure's <a href="http://clojuredocs.org/clojure.core/update-in" target="_blank">update-in</a>. <b><i>ii)</i></b> Zippers only deal with trees of left / right nodes. <b><i>iii)</i></b> So that left me with <a href="https://github.com/ekmett/lens/wiki/Examples" target="_blank">Control.Lens</a>. Now I can easily inspect and update the board like so. </font></div></div></div></blockquote><div><font color="#999999"><br></font></div></div></div><font color="#999999">Or you could use an two dimensional array of Positions instead (Data.Array).</font></div></div></div></blockquote><div><br></div><div>Hmm, probably. But the function's type signature below, seem to be the first order of business, no? </div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><font color="#999999"><span class=""><blockquote type="cite"><div><div dir="ltr"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>λ> board^<font><b><i>._</i></b><b><i>2._1</i></b></font></div></div><div><div>E</div></div><div><div>λ> set (<b><i>_2._1</i></b>) 42 board</div></div><div><div>((E,E,E),(42,E,E),(E,E,E))</div></div></blockquote><div><br></div><div>I can then parameterize _2 or _1 like so. </div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">λ> let a = _2<br>λ> board ^. a<div>(E,E,E)</div></blockquote></div></div></blockquote><div><br></div></span>You can probably find the type of _1, _2, _3 in the repl, and use that in your code below.</font></div></div></div></blockquote><div><br></div><div>The types (:t ...) of both of these are: </div><div><ul><li>a :: (Field2 s t a b, Functor f) => (a -> f b) -> s -> f t<br></li><li>_2 :: (Field2 s t a b, Functor f) => (a -> f b) -> s -> f t<br></li></ul></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div><font color="#999999"><span class=""><blockquote type="cite"><div dir="ltr"><div><div class="gmail_extra">But I can't figure out how to include that parameter's type into a <b><i>Position</i></b> type definition. I've tried these unsuccessfully. <br></div><div class="gmail_extra"><br></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div class="gmail_extra"><i>data Position = Int Int deriving Show  -- original type definition</i></div></div><div><div class="gmail_extra"><i>data Position = Simple Lens (Int a) (Int a) deriving Show  -- failing try 1</i></div></div></blockquote></div></blockquote><div><br></div></span>These are very suspicious. Did you intend to define constructors ‘Int’ and ‘Simple’? Maybe you meant something like:</font></div><div><font color="#999999"><br></font></div><div><font color="#999999">data Position = Position Int Int deriving Show</font></div><div><font color="#999999"><br></font></div><div><font color="#999999">In which case you’ll have a type called ‘Position’ and a constructor with the same name.</font></div><div><font color="#999999"><br></font></div><div><font color="#999999">Cheers,</font></div><div><font color="#999999">Bob</font></div><div><br></div></div></blockquote><div><br></div><div>So ultimately I want a function signature that lets me pass in a lens position. </div><div><br></div><div>-- 1. these 2 don't compile together</div><div><br></div></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div>data Position = Position Int Int deriving Show </div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><br></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div>move :: Board -> Piece -> Position -> Board</div></div></div><div class="gmail_extra"><div class="gmail_quote"><div>move board piece position = set (position) piece board</div></div></div></blockquote><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div><br></div><div>-- 2. and using the (:t ...) type definition abouve, none of these work</div><div><br></div></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div>move :: Board -> Piece -> ((Field2 s t a b, Functor f) => (a -> f b) -> s -> f t) -> Board<br></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>-- move :: Board -> Piece -> ((a -> f b) -> s -> f t) -> Board</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>-- move :: Board -> Piece -> (a -> f b) -> Board</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>-- move :: Board -> Piece -> (s -> f t) -> Board</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><br></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>move board piece position = set (position) piece board</div></div></div></div></blockquote><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>-- 3. so the below code compiles, but doesn't do me much good... I need Position to be a lens, such that I can use A) <b><i>set (position) piece board</i></b> , instead of B) <b><i>set (_2._1) 42 board</i></b></div><div><br></div></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><div>module Main where</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><br></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>import Control.Lens</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><br></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>data Piece = X | O | E deriving Show</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>type Row = [Piece]</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>type Board = [Row]</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><b><i>data Position = Int Int deriving Show</i></b></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><br></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><b><i>move :: Board -> Piece -> Position -> Board</i></b></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>move board piece position = board</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><br></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>main :: IO ()</div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div>main = putStrLn "Hello World"</div></div></div></div></blockquote><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div><br></div><div><br></div><div>Cheers mate :) </div><div><br></div><div><div class="gmail_signature"><div dir="ltr"><div style="font-family:arial">Tim Washington </div><div style="font-family:arial"><a href="http://interruptsoftware.com/" target="_blank">Interruptsoftware.com</a> </div><div style="font-family:arial"><span style="font-family:arial,sans-serif"> </span><br></div></div></div></div></div><br></div></div>