Online Book Reader

Home Category

Learn You a Haskell for Great Good! - Miran Lipovaca [69]

By Root 456 0
to test them. We’ve also explored the standard library functions in that way. Now we’re finally going to write our first real Haskell program! Yay! And sure enough, we’re going to do the good old Hello, world! schtick.

For starters, punch the following into your favorite text editor:

main = putStrLn "hello, world"

We just defined main, and in it we call a function called putStrLn with the parameter "hello, world". Save that file as helloworld.hs.

We’re going to do something we’ve never done before: compile our program, so that we get an executable file that we can run! Open your terminal, navigate to the directory where helloworld.hs is located, and enter the following:

$ ghc --make helloworld

This invokes the GHC compiler and tells it to compile our program. It should report something like this:

[1 of 1] Compiling Main ( helloworld.hs, helloworld.o )

Linking helloworld ...

Now you can run your program by entering the following at the terminal:

$ ./helloworld

Note

If you’re using Windows, instead of doing ./helloworld, just type in helloworld.exe to run your program.

Our program prints out the following:

hello, world

And there you go—our first compiled program that prints something to the terminal. How extraordinarily boring!

Let’s examine what we wrote. First, let’s look at the type of the function putStrLn:

ghci> :t putStrLn

putStrLn :: String -> IO ()

ghci> :t putStrLn "hello, world"

putStrLn "hello, world" :: IO ()

We can read the type of putStrLn like this: putStrLn takes a string and returns an I/O action that has a result type of () (that is, the empty tuple, also known as unit).

An I/O action is something that, when performed, will carry out an action with a side effect (such as reading input or printing stuff to the screen or a file) and will also present some result. We say that an I/O action yields this result. Printing a string to the terminal doesn’t really have any kind of meaningful return value, so a dummy value of () is used.

Note

The empty tuple is the value (), and it also has a type of ().

So when will an I/O action be performed? Well, this is where main comes in. An I/O action will be performed when we give it a name of main and then run our program.

Gluing I/O Actions Together

Having your whole program be just one I/O action seems kind of limiting. That’s why we can use do syntax to glue together several I/O actions into one. Take a look at the following example:

main = do

putStrLn "Hello, what's your name?"

name <- getLine

putStrLn ("Hey " ++ name ++ ", you rock!")

Ah, interesting—new syntax! And this reads pretty much like an imperative program. If you compile and run it, it will behave just as you expect.

Notice that we said do and then we laid out a series of steps, as we would in an imperative program. Each of these steps is an I/O action. By putting them together with do syntax, we glued them into one I/O action. The action that we got has a type of IO (), as that’s the type of the last I/O action inside. Because of that, main always has a type signature of main :: IO something, where something is some concrete type. We don’t usually specify a type declaration for main.

How about that third line, which states name <- getLine? It looks like it reads a line from the input and stores it into a variable called name. Does it really? Well, let’s examine the type of getLine.

ghci> :t getLine

getLine :: IO String

We see that getLine is an I/O action that yields a String. That makes sense, because it will wait for the user to input something at the terminal, and then that something will be represented as a string.

So what’s up with name <- getLine then? You can read that piece of code like this: perform the I/O action getLine, and then bind its result value to name. getLine has a type of IO String, so name will have a type of String.

You can think of an I/O action as a box with little feet that will go out into the real world and do something there (like write some graffiti on a wall) and maybe bring back some data. Once it has fetched that data for you, the

Return Main Page Previous Page Next Page

®Online Book Reader