Creating a Haskell File | |
When you first open HEAT it is ready for you to start creating a Haskell
file. Simply start typing away in the window to your left if you feel
adventerous, or continue reading if you're still a bit unsure about Haskell. Basic Declarations To learn some of the basics of Haskell simply follow this guide. In it we will talk through creating a couple of simple functions to see how Haskell is structured and works. Start by clicking on the "Add Function" button above. ![]() This will open a new window which has several input areas. We'll discuss what each of these are and what you can put in them now. Comment Here you should write a brief description of what the function you are writing is actually doing. For this example type in the following: square: a function to square a given number Name This is where the function name is inserted. The function name must start with a lower case letter and usually describes what the function does so for our example we use: square Type Haskell functions have a type which shows the values a function takes (its arguments) and the output it gives (the result). Our function to square a given number takes a single Integer as input and returns an Integer as output. Therefore the type is written as: Int -> Int Here you can see the first Int as the input and the second as Output. They are seperated by "->" Body The body is where the actual computation part takes place. In our case we simply square the number or to put it simpler multiply the number given by itself. square n = n*n Here you can see we have listed the function name again followed by a character representing the input. After the equals sign we have written what happens to the input to produce the result. Here we have n*n to give the original n figure squared. That's It!! Thats the first basic Haskell function done! If you've typed it all in correctly the program will now display the code in the window to the left formatted for Haskell. ![]() You'll notice there are some parts here that we did not type ourselves. The comment area has has two dashes added to show that it is a comment. Two colons have also been placed between the name and the type. This is used to signify that the function name "is of type" Int to Int. Another Example Try adding another function in the same way that this time takes a single Int as an argument and doubles it for the result. If you've put it in correctly the display should look like follows: ![]() Once you feel you've got the hang of functions and types have a look at evaluating expressions you create using HUGS. | |