Many of the Haskell tutorials I’ve looked through focus almost entirely on syntax with very little coverage on how to structure a program.
For example…
Here’s a bare-bones outline of a C++ application:
#include <iostream>
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
When I first started learning C++, examples like these helped me immensely in learning how to assemble individual pieces into working programs. Maybe I’m looking in the wrong places, but I haven’t been able to find any such examples that are as direct and simple for Haskell.
I already know A LOT of Haskell syntax. I can write recursive list comprehensions, and manipulate strings, integers, and lists out the wazoo.
In short: I just want to know what two subroutines and variable pass looks like in Haskell. If I can get some basic understanding on how to structure a Haskell program, I might finally be able to put all the syntax I’ve learned to some use.
A Haskell program’s structure is surprisingly simple. You have a
mainfunction which does IO, and that’s about it. So the basics:Now you can compile this into a simple program using something like:
and it should produce an executable called program.
In general, programs are structured as mostly pure functions doing the actual computation coupled with discrete parts of the code dealing with the IO. (There are of course exceptions to this, but the general idea of writing as much pure code as possible is fairly universal.)
Since almost everything is expressed as a bunch of pure functions, you do not pass variables between them–the functions communicate by passing arguments.
The part of your code that does
IOis anchored inmain. In most cases, all your IO is going to go through main; even if you write IO actions separately and give them names, they will ultimately be executed starting frommain.A “function group” in Haskell is called a “module”. You can have multiple modules, each in its own file:
Only the identifiers in the parentheses will actually be exported; the rest are hidden. If you do not include the parentheses at all, everything will be exported by default.
Save this file as
Blarg.hs. Now you can import it inMain:Another useful way to group functions is
where:This way
helperis only in scope forcomplicatedFunctionand also has access toa,bandcfromcomplicatedFunction.