Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Department of Computing

CS-213: Advanced Programming


Class: BSCS-8ABC
Lab 10: Functional Programming - Exploring Haskell

Instructors: Dr. Sidra Sultana and Dr. Sana Qadir


Note: The steps to be completed are labelled as S# and the questions to be answered are
numbered as Q#.

This lab explores the use of the very safe functional language Haskell.

S1: Install Haskell Platform


● Use instructions from ​https://www.haskell.org/platform/windows.html​ to install Haskell
Platform
● You can confirm installation of ​haskell-dev​:

S2: Getting started with Haskell


● Create a folder ​week11​. Create a new file called ​program1.hs​.
● You may use VS Code with ​Haskell ​and H ​ askell Syntax Highlighting
extension to edit this file.
● Open a PowerShell and navigate to the ​week11 ​folder.
● At the prompt, enter ​ghci​.

● Edit ​program1.hs​ as shown:


● Load ​program1.hs​ using ​:l program1.hs

● Find out value of ​maxInt ​and m


​ inInt​ as shown:

● Edit ​program1.hs​ as shown. Replace ​1000​ with the last four digits of your CMS ID.

● Save ​program1.hs​, reload it using ​:r​.


● Print the value of each of the three variables by entering them one by one at the prompt.
Take SCREENSHOT 1 showing the value of the three variables.

S3: Investigating a function


● Find out about the square root function by entering it at the prompt as shown:
● Edit ​program1.hs​, to find the square root of ​sumOfNums​ as shown:

● Save and reload (using ​:r​)


● Investigate some built-in mathematical functions (eg. ​exp​, ​log​, ​**​, ​truncate​, and
round​) and some built-in logical functions (e.g. ​&&​, ​||​, and ​not()​).
● Add code to calculate the square root of your CMS ID rounded to the nearest integer
value.
● Take SCREENSHOT 2 showing the value.

S4: Lists
● Understand the following code. It may help to test out some of these examples:
● Write one line of Haskell to do ​each​ of the following:
a. Create a list ​myCMSID​ consisting of the digits in your CMS ID. There should be at
least eight digits in this list. Reverse this list.
b. Create a list ​myNewCMSID​ by prepending this list with the four digits that make
up the year in which you were born.
c. Create a variable ​maxDigit ​and assign it the maximum digit in the list
myNewCMSID​.
d. Create a variable ​is8 ​that checks if the digit ​8​ occurs in ​myNewCMSID​.
Take SCREENSHOT 3 showing your code and the value of all four of these variables.
● Write one line of Haskell to do the following:
e. auto generate and filter a list of numbers between 2000 and 5000 that are
divisible by 9 and 13.
Take SCREENSHOT 4 showing your code and the output.

S4: Going deeper


● Find out what the ​zipWith ​function does. Write ​one ​line of Haskell that:
f. uses ​zipWith ​ with ​*​, ​myCMSID​, and the list created in ​e​.
Take SCREENSHOT 5 showing your code and the output.
Q1: What do you notice from the output about what the ​zipWith ​function does and the
length of the output produced.

● Write ​two ​lines of Haskell to create the multiplication table shown below:

Take SCREENSHOT 6 showing your two lines of code.


Hint​: the first line of the code should generate a ​multi3List​ list.

You may use the following for reference “​Chapters - Learn You a Haskell for Great Good!​”

SUBMISSION: Instructions
● Convert the following into ​one ​PDF file and upload on the LMS submission link by the
due date:
○ Screenshot 1 to 6.
○ Answers to Q1

You might also like