M3L3 Searching A Word

You might also like

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

FUNCTIONAL PROGRAMMING

MODULE 3 LESSON 3
SEARCHING FOR WORDS

 Finding a string in another string with isInfixOf


 Map
 Reverse
 Bool and Maybe

We're going to search for words in the grid that we created in the module in order to do
that, we're going to look at how to transform this using functions map and reverse and
we'll see how to use the boolean and maybe data types.

isInfixOf

We want to search the words in the languages list like Haskell in the grid from left to
right, from right to left, top to bottom, bottom to top and of course diagonally.

Let's simplify it as much as possible and say that for now we look for a word just from
left to right. In the case of Haskell we can see that looking for it on the first line of the
grid wouldn't find the result. Nor on the second line but when we get to the third line
we actually would find the word HASKELL.

So let's have a look at the Hoogle search engine and see whether there is a function that
takes a string and another string and returns a boolean and we can see here that
Hoogle isn't giving us specific results for string to string to boolean but it is giving us
results for a list of a list of a and a boolean.
The string is simply a list of characters. So isInfixOf takes two lists and returns true if
the first list is contained inside the second list and we can see that this function is
defined in Data.List.

So let's see if this function does what we want.


If we import Data.List in GHCi we can see isInfixOf will find the word inside the line that
we're looking for.

Because this is a function that takes two arguments one on the left and one on the right
there is another style of calling functions that is quite handy and that's to surround it in
back ticks.

That allows us to put the function in the middle of the two arguments.

The function will return whether the word isInfixOf is contained in the line.

In order to compile this we would need to import Data.List and the function isInfixOf as
the parameter.
MAP

We need to check whether the word is found on every line in the grid. And we can do
that by using the map function.

Imagine a simple function like length which takes a string and returns an integer the
length of that string.

Map can be used to take a function like length and a list of strings and return a list of
integers. For example here we might be mapping the function length over a list of
strings and returning a list of integer lengths of those strings.

We know that the grid is a list of lines.


We could apply a function to that.

Mapping the length function over the grid will return a list of integers. Each length is 15
because it's a square grid.

Go back to our code and write out the definition for the findWord function.

So we've seen how to search for words from left to right and we're simply using a
function which is a synonym for isInfixOf.

REVERSE

We could write a function to search the grid from right to left. But in fact it would be
much simpler to simply use the same function but reverse the grid.

If we were to reverse one of these lines:


We could search that line with a programming language.

So if we take a line that we know has the name of program language written backwards
then we'd be able to find that using exactly the same function.

If we try to reverse the grids obviously we would just get the grade from top to bottom
the wrong way round and that won't help us find any new words within there.

What we actually want to do is to reverse each line. In this case we could use the map
function. The map reverse grid would apply reverse to every line within the grid and
return a new grid.
You can see here that we have the language Perl now visible.

To search the grid from both left to right and right to left instead of only searching
within just the untransformed grid we could create a new grid that contains both all the
lines within grid and the new grid created by mapping the reverse function over every
line of the grid in the expression or map findWord in line word over lines.

As you can see we now have a findWord function that will take a grid and a word and
will return a true value if it can find that word either running from left to right or from
right to left.

It would be nice to know which of these words we can find in the grid. And now that we
know about map we know that we could run some function against languages and we
could find out those words and know that we have a findWord function.
This result isn't useful because although we get a list of false and true values and we can
see here that we've matched three languages against the grid. It's a little annoying to
see which one, obviously we can see that basic wasn't found, cobol wasn't found, C-
Sharp wasn't found Haskell was.

But a much nicer outputs would actually be just the list of languages that were found. It
seems like perhaps boolean isn't in fact the best return result for findWord.

MAYBE

If we were to create a new function findWords that takes a grid and a list of strings in
this case we could return maybe string.

And if you remember maybe allows us to write for example just Haskell or nothing and
you can think of nothing as being false and just Haskell as being a true value.

Instead of returning this expression we can assign it to a new variable and we could
return a value based on whether we did in fact find the words.

We also need to add findWords to the list of exports.

And now we get a much more useful list.


We have the expressions, Nothing, for all the words that were not found and just Haskell
just Perl and in just php, for those words that were found.

That's quite nice but what we really want would be only those words which have just
values. I'm going to recommend that we go to Hoogle and find out if there's anything
in the standard library.

So if we had a list of Maybe something we wanted to get a list of just something and we
can see that there is a function catMaybes that takes a list of maybes and returns all the
just values.

So this function is defined in data.Maybe so let's import that function,

And as you can see findWords function now returns only those languages that we can
find on the grid.
NOTE: WORKING FILES FOR MODULE 4 IS UPLOADED AT THE CLASSROOM. YOU CAN CHECK THE
COMPLETE CODE UPDATES OF THE LIB.HS AS THE TOPICS ARE MOVING ON.

END OF THIS MODULE.

You might also like