08 Jun 1st Programming Coursework in Haskell Language.
Question
School of Computer Science, University of Nottingham
G51FUN Functional Programming, Spring 2012
Neil Sculthorpe
Coursework I, Part 1/5
Thursday 2nd February 2012
Deadline: Friday 10th February 2012, 4pm
This exercise sheet is worth 20% of the first coursework. It is assessed on a simple pass/fail basis: if you complete the sheet you will receive full marks, otherwise no marks.
You should attempt to complete the exercises in your own time, but if you get stuck you can ask for help during the lab sessions.
Assessment will be carried out by oral examination during the lab sessions (nothing needs to be handed in). When you have completed the exercises you should ask a tutor to examine your solution. The tutor will then ask you some questions to test your understanding.
Note that tutors will only be available to assess your solution during the official lab session (Fridays, 2pm to 4pm, A32).
The most commonly used Haskell compiler is the Glasgow Haskell Compiler (GHC). GHC also provides an interactive interpreter (GHCi), which will be the main tool used in this module. The recommended way to write Haskell programs is to have two windows open: one for a text editor to write your code, and the other for GHCi so that you can regularly load and test your code.
1. Open a text editor of your choice. On the Windows machines in the lab I recommend Notepad++. If you’re using your own machine and are familiar with Emacs, then I recom-mend installing the Emacs Haskell Mode and using that.
2. Type in the following function definition:
double x = x + x
3. Save your file as
Script1.hs
The name does not really matter, but it is important that it has a.hs file extension (indi-cating that it is a Haskell source file).
4. Load your file into GHCi. If you’re using Windows on the lab machines, then double-clicking on your file will open GHCi and load the file (provided you correctly saved it as a Haskell source file). If you’re using a command line interface (e.g. the School’s Linux servers), then navigate to the directory containing your file and type:
ghci Script1.hs
In either case, GHCi should load and you should see something similar to this:
[1 of 1] Compiling Main ( Script1.hs, interpreted ) Ok, modules loaded: Main.
*Main>
5. Test your function on some numbers. For example, type: double 7
6. Add a new function to yoursource file by typing (on a new line):
quadruple y = double (double y) Then save your file.
7. To test the new function, you need to reload the file into GHCi. This can be achieved by giving the command:reload (in GHCi). Now test the quadruple function on some numbers.
8. Add the following functions to your source file, and then test them in GHCi:
smallest x y =if x< ythen xelse y largest m n =if m> nthen melse n
Don’t forget to reload the file before testing it, or to save the file before reloading it.
9. Tab characters can cause problems when a file is used by different programs, as some programs interpret tabs as 4 spaces, some as 8 spaces, and some as a single character. Haskell syntax uses layout as an alternative to curly braces and semicolons, which means the width of a tab can affect the meaning of a program.The best way to avoid problems is not touse tab characters in your source files.However, if you really want to use the tabkey when writing Haskell code, then ensure that your editor either uses tabs of width 8, or automatically converts tabs to spaces. This can be achieved in Notepad++ by going to “Settings/Preferences”, selecting the “Language Menu / Tab Settings” tab, and then changing either the default settings or Haskell-specific settings.
Now add and test the following code, making sure that the l and s after thewhere line up in the same column.
diff x y = l − s
where l = largest x y s = smallest x y
Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.
About Writedemy
We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.
How It Works
To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Are there Discounts?
All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.
