<div dir="auto">Hi Maxim,</div><div dir="auto"><br></div><div dir="auto">The scope of this question falls outside this beginners list, which tends toward questions about haskell syntax (see other active thread).</div><div dir="auto"><br></div><div dir="auto">You will typically find more response on the haskell-cafe list, which you might want to resend your query to.</div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Dec 21, 2020 at 11:01 PM Maxim Frolov <<a href="mailto:maxim.frolov.07@gmail.com">maxim.frolov.07@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)">
<div>
<div name="messageBodySection">
<div dir="auto">Hi All,<br>
<br>
I am new to Haskell and got stuck while experimenting with minimax algorithm (tic-tac-toe game). Just for learning I am trying to avoid external modules and use only the standard Prelude library.<br>
<br>
Here is the code snippet:<br>
<br>
<span style="font-family:Menlo;font-size:11px">type Pos = (Int,Int)</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px">-- Players are O (minimizing) and X (maximizing), B is for the draw (blank), I and J are used for -INF and +INF respectively</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px">data Player = I | O | B | X | J</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> deriving (Eq, Ord, Show)</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px">type Grid = [(Pos,Player)]</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px">data Tree a = Node a [Tree a]</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> deriving Show</span><span style="font-size:11px"><br></span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px">minimax :: Player -> Player -> Tree Grid -> Tree (Grid, Player)</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px">minimax _ _ (Node g [])</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> | wins X g = Node (g,X) []</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> | wins O g = Node (g,O) []</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> | otherwise = Node (g,B) []</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px">minimax a b (Node g ts)</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> | turn g == X =</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> let ts' = [minimax alpha b t | t <- ts, alpha < b]</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> ps = [p | Node (_,p) _ <- ts']</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> alpha = maximum (a:ps)</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> in Node (g, alpha) ts'</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> | turn g == O =</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> let ts' = [minimax a beta t | t <- ts, a < beta]</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> ps = [p | Node (_,p) _ <- ts']</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> beta = minimum (b:ps)</span><span style="font-size:11px"><br></span><span style="font-family:Menlo;font-size:11px"> in Node (g, beta) ts'</span><span style="font-size:11px"><br></span><span style="font-size:11px"><br></span><span style="font-size:11px"><br></span>The function call is like:<br>
<br>
<span style="font-family:Menlo;font-size:11px">minimax I J tree</span><span style="font-size:11px"><br></span><span style="font-size:11px"><br></span><span style="font-size:11px"><br></span>It looks like I got a recursion loop. Could someone advise how to approach the problem?<br>
<br>
Thank you,<br>
Max<br>
<br>
<br></div>
</div>
</div>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">-- Kim-Ee</div>