Iteration vs RecursionFiled under: algorithm
One of my goals is to write Haskell at a decent pace, but am still much more fluent at Python. I wanted to translate an algorithm that I wrote awhile back in an iPython Notebook, of traversing in an outward spiral in Python.
Starting at (0,0), the moves increment in a pattern like so: 1 up, 1 right 2 down, 2 left 3 up, 3 right 4 down, 4 left ...
I drew a pretty diagram to show how it looks like graphically.
In order to track both the direction and number of steps in that direction, I used two loops. One for the number of steps taken, and one for number of steps taken in a direction. I also created a couple of utility functions (see below) that handle the repetitive steps such as
move(coordinates, direction) and
change(direction). The additional goals in writing the solution is to make it more readable and easier to maintain, and splitting out these functions makes the spiral function eaiser to digest. Here’s how my solution looks with while loops:
In effort to translate this into Haskell, I basically created similar utility functions. The first utility function is
move which takes the direction and current coordinates and returns the next direction. It’s fairly similar in both languages as we’re working with conditionals.
Next, I have a function that decides on the direction and this is also conditional so it looks imilar. I am using the list structure in Python, whereas I’m using the data type in Haskell.
With these utility functions I can go ahead and think about how to keep track of the steps taken. What I want to do with the (x,y) coordinates is figure out when to turn, thus will have to track the goals for the step. So far I have this much written out, for recursively taking steps until the specified number n. I still need to track the step goal for the level so that directions can be specified.
Anyway, that’s how far I got this week and will continue to plug away at this and try to finish the solution soon.