You are on page 1of 5

FUNCTIONAL PROGRAMMING

MODULE 3 LESSON 2
SETTING UP THE WORD GAME GRID

 Declaring a grid
 Printing it to the screen
 Discovering Haskell functions with Hoogle
 unlines

DECLARATION OF GRID

The Lib.hs module below which we’d started to work on in the previous module.

Inside the Lib module is the declaration of grid. In Haskell, lists are declared using the
square brackets and they're separated by commas.

This is a very typical Haskell formatting convention where you align the commas and the
opening and closing braces on the same column.
You don't have to type your code in this way but it is fairly common. The reason we've
put these on different lines is so that the strings will line up making this look like a word
grid. You can see in the grid the names of the programming languages.

Notice that instead of filling out the whole grid with a jumble of random letters, instead
all of the letters that are not the name of a programming language be the letter
underscore. So that we can see what's going on and we can fill those underscore with
random letters later.

Aside from the declaration of grid, a list of languages is also declared.

Just like the grid above, this is also a list of strings. But in this case the strings are of
different lengths. This isn't a grid. This is just a list of strings.

As we've added these declarations languages and grid, we need to export them from
the module so that they can be seen in the executable.

When stack gchi is run, we can type grid and languages and will see the results.
In the languages list that is perfectly reasonable, we can see the list of the languages we
write during the declaration. But in the case of a grid we'd really like to see that it is
formatted as a block rather than a comma separated list of lines.

HOOGLE https://hoogle.haskell.org/

Hoogle is a Haskell API search engine, which allows you to search the Haskell libraries
on Stackage by either function name, or by approximate type signature.

In the hoogle search box, type the type signature and see if there are any functions that
match that signature.

There are a whole load of suggestions. Some of them don't quite match.
It's being helpful by giving us signatures which are related.

The first function in the list of results here looks really promising.

It joins lines after pending a terminating new line to each. And that's exactly what we
want to do.
unlines

If we try unlines grid we'll see that we have a string which has joined up all of our lines
but this time its added slash n (\n) in between each line and that's the new line.

If we putStrLn (unlines grid) that will output a grid exactly in the way that we want it
to.

In the Lib.hs we have some changes and some additional functions.


And some changes in the Main.hs

We have exported output grid in the Main.hs. We could run output grid against the grid
variable that we've defined in Lib.hs. And now our word game will simply outputs the
grid to the screen.

END OF THIS MODULE

You might also like