Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

FUNCTIONAL PROGRAMMING

MODULE 3 LESSON 4
SEARCHING IN ALL DIRECTIONS

 Reverse
 Transpose
 Writing a recursive function

In the previous module we talked on how to search the grid for words from left to right and
right to left. In this module we're going to extend that work and look at how to search in all
directions.

We'd like to search a line containing the characters of the first column the second column the
third column and the fourth column and so on. It's a good idea to look at Hoogle and see what
kind of functions are available.

We know that we have a grid which has a list of a list of characters and we want to return
another grid but with the characters in a different order. Let's see what kind of existing
functions there are to map from one sort of grid to another.

It’s a transpose and you can see that it transposes the rows and columns of its arguments.

Again we need to import data not list. And we can see of course that it goes from a grid to
another grid. So if we ran transpose grids we can see already here that words like C-Sharp and
Python are now jumping out at us.

So let's try doing an output grid of the normal transposed grid and you can see here that
Haskell and Perl and PHP are running.
And if we run that on the transposed grid we'd still see Haskell running from top to bottom.

And now C-Sharp and Python are running from left to right.

We've already got the variable called lines that contains the grid plus the reversed grid. Let's
extract that into a function and we can see that that has exactly the same effect as before.

(Taken this from the Lib.hs which is in the Module 4 > words > src > folder)

We need to add transpose and now when we call findWords grid languages we can see a
whole load of new languages appearing.
We haven't yet found all of them but we are now doing from left to right, right to left top to
bottom and bottom to top so we've seen the grade transposed. Now we want to search
diagonals as well.

So for example we might want to search the line containing just the letter X and then I, L and so
on. Before we continue it's really worth having a think about how you might want to do this.

If we simply offset each of the rows we could now read down the columns like so.

And in order to do that we could simply use the transpose function that we've already used.
So the first thing that we'd want to do is to write a function that skews a grid by prepending it
with some character. So let's have a look at creating a function call skew and that will take a
grid and it will return a skewed grid that we can easily then call transpose on.

We want to have a recursive algorithm and so when you're writing a recursive algorithm you
always need to remember the base case. If we had an empty grid we would simply return an
empty grid and the other option is that we would have a grid which contains a first line.

You can see here that the first line is now shorter than all the rest of them because the first line
has been left unchanged and all the others have an extra character at the beginning. But that's
not good because we wanted each successive line to have an extra underscore before it. So
instead of simply mapping and over the rest of the lines we want to recurse into the skew
function.

Now we can see that each successive line gets an extra character of indentation.

Now you can see for example that scheme can be read from top to bottom whereas C-Sharp
that was previously from top to bottom is now running diagonally so we could now run
transpose skew grid and we would see something that doesn't look very much like a grid at all
but does have SCHEME and COBOL running backwards in it.

So this is looking like we'd be able to plug it into our getLines function and suddenly a whole lot
of new words are going to show up in our findWords routine.

So diagonal is transpose of skew great and the lines are not just horizontal and vertical but also
the diagonal lines.

And you can see that SCHEME and COBOL are also found but that's not all the languages were
missing BASIC and Ruby.
We can see that SCHEME and COBOL were running in the direction opposite to RUBY and
BASIC. We're going to need to do a different transpose.

We will modify the getLines function.

Let's see if this will give us the results that we want.

We are still missing BASIC and Ruby.

Just like when we run main, we also get the diagonals (in a forward order) when we output the
transposed grid, where it still contains SCHEME and COBOL.
We need to reverse the grid like this.

And at this point the diagonalize is going to find Ruby and Basic.

Modified:

And now we can see Basic and Ruby in our list.

To make it nice we need a new function which we call diagonalize which will take a grid and
return a grid.

END OF THIS MODULE

You might also like