Sunday, August 12, 2012

Haskell Chapter 1: Getting Started

     If you're not using Linux (or Unix) already, you should be. Sure, you can install Haskell on just about any kind of pc, but the Linux OS was designed by coders for coders. Trying to learn programming on Windows or Mac is a bit like trying to become a gourmet chef by getting a job at McDonald's. Why would you do that? Linux is Le Cordon Bleu and everything else is McDonald's. Besides, modern Linux distros cost nothing and are actually easier to install and setup then their corporate counterparts. If you're worried about transitioning to a new OS, don't be. You'll find modern versions of Linux work in virtually the same way as the OS you are used to. Particularly if you stick to the Ubuntu-family; you'll quickly see that there really is no learning curve to speak of. I'll even pick a distro for you, click here: Mint 13 MATE
   
      In Linux, you automatically get an unfathomably large library of free applications which usually work better than the paid kind, have better support (strange but true), and are open-source; so they are constantly being debugged, improved and upgraded. To get the software resources that automatically come with a good Linux distribution on an OS like Windows, you would need to pay tens-of-thousands of dollars in licensing fees. Then there's the fact that Linux is the most reliable OS ever made. The reduction in frustration-level alone makes it worthwhile. There's no reason to put it off, make the switch now! If you still insist on spending money on crappy ripoffs of programs that were originally written for either Linux or Unix, then you can go here and find an installer: the Haskell Wiki.

     Now, if you have Ubuntu or Mint you can just open Synaptic Package Manager and search for 'ghci'. Install the latest version. See how much easier that is? Regardless of your system, the next steps are relatively the same. 

     One final note before we begin: You will see a lot of quotations in any text dealing with programming. This is a little confusing when you are starting out. When I tell you to type something a certain exact way, I will use single quotations to separate that code from the rest of the neighboring sentences. 

     I don't want you to actually type the single quotation marks themselves. Instead, type what's in-between the single quotation marks. Like, if I say type 'ghci', you type this: ghci . On the other-hand, if you see double quotation marks ("), go ahead and type those. Haskell uses double quotation marks to process certain types of data. If there are no surrounding lines of text (see step 7 below), I will omit the single quotes for simplicity's sake. Likewise, Haskell will present a 'prompt' which prompts you to type something. It usually takes the form of some library (like Prelude) followed by ' > '. I don't won't you to type 'Prelude>'. I do want you to type the text that occurs after it -- like 'Prelude> let x = 9'.

OK, let's Haskell!


1. Create a folder for your Haskell programs. This is a good time to start using the Haskell naming convention. In Haskell, we never capitalize the first word in a name. However we always capitalize the subsequent words -- all the while leaving out any spaces because Operating Systems don't like spaces (go-figure). So my Haskell program folder is called haskellPrograms. If you prefer, HaskellPrograms and Haskell_Programs will work for file names too.
2. Open up your terminal application and navigate to /haskellPrograms. This is important. Once you run the haskell interactive environment (ghci) in the next step, you will eventually need to load-up the programs you write. In order to do this, ghci needs to 'know' what programs you are referring to. It will automatically look in whatever folder you were in when you called ghci. In this case, that folder will be haskellPrograms.
3. Open your favorite text-editor (I use KATE). If you are using Linux or a Linux ripoff (Mac), you can just switch to another empty workspace with the click of a button. If you are still using windows, you must minimize your current screen etc, open the Programs menu, then open up some text editor and finally maximize the browser window every time you need to see this blog. Oh wait, you also have to do all that stuff with your terminal application too. Why are you still using Windows?
4. Go to the Edit>Preferences section of your text-editor. Now, locate the 'tabs' settings and change it to this: Tabs>Use Spaces>Number of Spaces = 8. Next, set the indent to Use Spaces>Number of Spaces = 2. This is crucial. Haskell is very sensitive to tab spacing. There is still no standard for tab spacing; some companies use 3 spaces, some use a fixed pixel width. If your text-editor doesn't use the exact settings above, you are in for a world of frustration! However, if you are using KATE, you can skip this step: KATE comes with the correct spacing out-of-the-box.
5. Open a new file in your text editor. Save the blank file as test.hs within your /haskellPrograms file. Dot hs is the suffix for Haskell source-code. Later when you compile your first program, the suffix will change. But, the suffix you will work with the most is ' .hs '. Now, you are ready to enter code into test.hs and load it into ghci.
6. Go back to your terminal and type 'ghci '. This will launch the ghci interactive environment. This is a program that lets you experiment with the Haskell language without compiling a program. Using ghci is a little bit different from writing an actual Haskell script, so I will try to concentrate on scripts instead -- that way you don't get too confused.
7. Go back to your text-editor and write this all the way on the top-left corner (do NOT skip a line) of the 'test.hs' file, which you should already have open:


main = do
    putStrLn "Hello World!"

8. Now save test.hs. Go back to ghci (in the terminal) and type this ':l test.hs'. Colon l is the command for "load the following file", in this case the file we want to load is test.hs.
9. Now type 'main' and observe! If everything was entered into your text editor as described above, you should now see 'Hello World' on the line below, like this:

*Main>main
Hello World!
*Main>

     Ya, I know what your thinking: that's a pretty lame program dude. Just bear with me here, it will get better real soon. For now, you have accomplished that famously devilish programming task: Haskell IO! Take a short break, drink some water, have a cookie. Next I'll explain the basic Haskell operations like arithmetic and IO. Using these, you can start experimenting with ghci and get a feel for what it's like to program in Haskell. But before we do that, you'll want to know how to leave your current ghci session and possibly load up another one: type ' :quit '. Be careful however, this doesn't always work! Some programs will endlessly read your keyboard input; even reserved commands like :quit.

    One final word about the 'Prelude>' prompt. In Haskell, whenever you load a program module or library of program modules, Haskell conveniently displays the module or library that you have successfully loaded at the prompt. When you first start-up 'ghci', it automatically loads the standard-library known as 'Prelude'. Prelude is a collection of modules which the Glasgow Haskell folks believe to be vital to daily usage. It is a large library and it changes from time to time as the Haskell committee vote to insert or remove modules. There are other libraries which are just as crucial. In fact, I am often perplexed to find that basic common-sense modules are missing from Prelude but available in other places like Data.Char.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.