I am trying to write a recursive function that returns List <int * int>, but I am having some problems with the syntax.
The function should return an empty list at the end of the recursion, otherwise the tuple (int * int) merges with the list returned by the recursive call to itself:
let rec foobar () : List<int * int> = if (recursionIsEnded) then [] else foobar () :: (10, 10)
Can someone explain to me what I'm doing wrong?
EDIT:
I will try to give more detailed information. Actually my function is a bit more complicated. I iterate through a 2d array and build a list of tuples with array index elements that satisfy a certain condition. Actually my code is:
let rec GetSameColorNeighs(grid : Option<Ball> [,], row : int , col : int, color : Microsoft.Xna.Framework.Color) : List<int * int> = if (row < 0 || col < 0 || row > MaxLineNumber - 1 || col > BallsPerLine - 1) then [] else let ball = grid.[row,col] match ball with |Some(ball) -> if (!ball.visited = false || not <| ball.color.Equals(color)) then [row , col] else ball.visited := true (row,col) ::GetSameColorNeighs(grid, row + 1, col + 1, color) :: GetSameColorNeighs(grid, row - 1, col - 1, color) |None -> []
So, 2 more questions :):
How to change the following line to compile it?
(row,col) ::GetSameColorNeighs(grid, row + 1, col + 1, color) :: GetSameColorNeighs(grid, row - 1, col - 1, color)
Is there a better way to do this?
I don't need the order of the list items.
source share