<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<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>
</body>
</html>