0

I am beginning to sketch out the structure of a recursive algorithm that will build an adjacency list of hex tile intersections in a Settlers of Catan style game board. However that is not the immediate problem I need to solve.

The issue I'm having can be found in the output I've pasted below the code sample. Some the values in the first and last rows of output appear to be strings for no obvious reason.

I'm hoping a more seasoned Elixir developer will recognize the reason for the corrupted output and be able to explain why it occurs and what to do about it.

  def recurse_outer() do                                                                                                                             
    list = [Enum.into(0..5, []),                                                                                                                     
            Enum.into(6..23, []),                                                                                                                    
            Enum.into(24..53, [])                                                                                                                    
           ]                                                                                                                                         
    Enum.map(list, &recurse_inner(&1))                                                                                                               
  end                                                                                                                                                

  defp recurse_inner(list, index \\ 0, result \\ [])                                                                                                 
  defp recurse_inner([head | tail] = list, index, result) do                                                                                         
    next =                                                                                                                                           
      case tail do                                                                                                                                   
        [next | tail] -> next                                                                                                                        
        [] -> :no_next                                                                                                                               
      end                                                                                                                                            

    adjacencies = [head, next]                                                                                                                       

    recurse_inner(tail, index + 1, [adjacencies | result])                                                                                           
  end                                                                                                                                                
  defp recurse_inner([], _, result), do:                                                                                                             
    Enum.reverse(result)

output:

iex(159)> Board.recurse_outer
[[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, :no_next]],
 [[6, 7], '\a\b', '\b\t', '\t\n', '\n\v', '\v\f', '\f\r', [13, 14], [14, 15],
  [15, 16], [16, 17], [17, 18], [18, 19], [19, 20], [20, 21], [21, 22],
  [22, 23], [23, :no_next]],
 [[24, 25], [25, 26], [26, 27], [27, 28], [28, 29], [29, 30], [30, 31],
  [31, 32], ' !', '!"', '"#', '#$', '$%', '%&', '&\'', '\'(', '()', ')*', '*+',
  '+,', ',-', '-.', './', '/0', '01', '12', '23', '34', '45', [53, :no_next]]]

0 Answers0