-1

Im currently trying to write a minimax algorithm using tree in haskell for a connect four game where it takes maximum tree when its ai's turn and minimum when players. I've tried to find how use the tree structure in general such as : create, write, read and traverse however the internet returned with no helpful result as I'm declaring the tree in a specific way

The Tree structure that I'm implementing is defined as follows

data Tree a = Node a [Tree a ]

I understand how I can create a tree variable like

t :: Tree Int 
t = Node 0 [Node 1 [], Node 2 []]

But i do not know how to actually use it or manipulate or access the node's value please prodvide any or all examples and explanations

Thanks

P47HF1ND3R
  • 45
  • 7
  • 1
    There aren't any *particular* rules or techniques for working with your `Tree`; it's an ordinary data type, so the same rules work with it that work with data types in general. If this is an exercise from a course or a book you're working through it has almost certainly already covered those rules in general, and part of the purpose of the exercise is to get you applying that general knowledge to a new type, rather than needing to be told exactly how to work with every type from scratch. I would suggest revisiting earlier material on data types, pattern matching, and general Haskell syntax. – Ben Mar 12 '23 at 04:01
  • Note that you can get a handful of utility functions for your data type “for free” like this, courtesy of the compiler: `data Tree a = Node a [Tree a] deriving (Eq, Show, Functor, Foldable, Traversable)` – jpmarinier Mar 12 '23 at 18:12

1 Answers1

2

To access a tree value you use pattern matching, often together with recursion and/or other library functions.

For instance, let's sum all the values in a Tree Int.

sumTree :: Tree Int -> Int
sumTree (Node x ts) = x + sum (map sumTree ts)

Here the tree value is dissected into the root value x and the list of subtrees ts. Then map sumTree ts recurses computing the list of all the sums of the subtrees. We sum that list of integers into a single one, and then we add x to conclude.

In this example we did not have to provide a base case, since when the list of subtrees is empty map does not perform any recursive call.

If you are new to Haskell, I recommend you study pattern matching in a tutorial like LYAH. It is one of the most important techniques to know in Haskell.

chi
  • 111,837
  • 3
  • 133
  • 218
  • Since I'm writing a minimax algorithm surely it would be better to know individual values at a certain node position rather than the sum or is the sum stored into the subtrees, sorry I'm still slightly confused – P47HF1ND3R Apr 07 '23 at 07:37
  • @P47HF1ND3R It's fairly similar. For the minimum one would instead write `minTree (Node x ts) = min x (minimum (map sumTree ts))` which is essentially the same code as above. For minmax, you need to alternate min and max in each recursive call, so you probably need to add an additional boolean argument which you negate at each recursive call. You should post your attempt in a new question asking to receive more help. If this exercise is too hard for you right now, it can also help trying to solve easier exercises on trees first (e.g. max depth, count the elements, etc.). – chi Apr 07 '23 at 08:28
  • Ahhh thank you very much sorry I thought there's an implementation for minimax i didn't think you can just alternate between min and max thanks so much :) – P47HF1ND3R Apr 09 '23 at 22:32