0% found this document useful (0 votes)
350 views

Introduction To Functional Programming With Haskell Exercise Sheet 1

This document provides an introduction and instructions for completing exercises in a Haskell programming course. It instructs the reader to: 1) Open a terminal, navigate to a workspace directory, start the Haskell interpreter ghci, and create a file called Exercises.hs containing a test function. 2) The exercises involve writing functions for: squaring a number and summing squares; calculating list length recursively; dropping the first n elements from a list; and taking the first n elements of a list. 3) When making changes, reload the Exercises.hs file in ghci to test the solutions.

Uploaded by

Paulo Torres
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
350 views

Introduction To Functional Programming With Haskell Exercise Sheet 1

This document provides an introduction and instructions for completing exercises in a Haskell programming course. It instructs the reader to: 1) Open a terminal, navigate to a workspace directory, start the Haskell interpreter ghci, and create a file called Exercises.hs containing a test function. 2) The exercises involve writing functions for: squaring a number and summing squares; calculating list length recursively; dropping the first n elements from a list; and taking the first n elements of a list. 3) When making changes, reload the Exercises.hs file in ghci to test the solutions.

Uploaded by

Paulo Torres
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Introduction to Functional Programming with Haskell Exercise Sheet 1

Starting Haskell and running your rst program

Start Linux and open a terminal. Within the terminal, cd to the directory in which you want to store your work. Start the haskell interpreter by typing ghci. Use your favourite text editor to create a le called Exercises.hs in the same directory with this content: module Exercises where test :: String test = Hello World! Going back to ghci, load your new le and test it like so:

> :load Exercises > test Add your solutions to the following problems to Exercises.hs. When you make changes you need to reload the le in ghci:

> :reload Exercises

Exercises
1. Write a function square :: Int -> Int which returns the square of a number. Use square to write a function sumsquare :: Int -> Int -> Int which returns the sum of the squares of its two arguments.

2. Write a (recursive) function length :: [a] -> Int which calculates the length of a list. Think about the base case of the empty list and the case when the list contains elements. 3. Write a function drop :: Int -> [a] -> [a], where drop n xs returns xs with its rst n elements removed. 4. Write a function take :: Int -> [a] -> [a], where take n xs returns the rst n elements of xs as a list (if xs contains less than n elements, your function should return all of xs).

You might also like