Computer Science 340
Programming Languages

Fall 2023, Siena College

Lab 4: Haskell Practice
Due: 11:59 PM, Thursday, October 5, 2023

In this lab, you will experiment with Haskell programming.

You may work alone or in groups of size 2 or 3 on this lab. However, in order to make sure you learn the material and are well-prepared for the exams, those who work in a group should either collaborate closely while completing the problems or work through the problems individually then discuss them within your group to agree on a solution. In particular, the "you do these and I'll do these" approach is sure to leave you unprepared for upcoming tasks and the exams.

Learning goals:

  1. To gain experience with the basics of the Haskell programming language.
  2. To learn about programming in a functional programming paradigm using Haskell.
  3. To learn about programming using list comprehensions.
  4. To gain experience with tuples in Haskell.

Getting Set Up

In Canvas, you will find a link to follow to set up your GitHub repository, which will be named haskell-lab-yourgitname, for this lab. Only one member of the group should follow the link to set up the repository on GitHub, then others should request a link to be granted write access.

All GitHub repositories must be created with all group members having write access and all group member names specified in the README.md file during class, Thursday, September 28, 2023. This applies to those who choose to work alone as well!

Haskell Basics and Functions

In the Learn You a Haskell For Great Good! tutorial, we saw the succ function. Unsurprisingly, there is a corresponding pred function. Give it a try.

Practice Program: In a file preds.hs, write a Haskell function preds that takes a number n >= 0 and a value x (of a type which can be passed as a parameter to the pred and succ functions), and returns the result that is the same as calling pred n times starting with the value x. (5 points)

For example, the function calls

preds 4 10
preds 5 'p'

should return 6 and 'k', respectively.

List Manipulations

For the practice programs in this section, you will work in the file listpractice.hs, where you will find a program that defines a main function that creates and prints out two lists.

Practice Program: Using only combinations of the four fundamental list operations, head, tail, init, and last, add 6 more printouts to the program, one to print out each of the numbers in a (as a number, not a list). (6 points)

Practice Program: Using only combinations of the take and drop functions, add 4 more printouts to the program, one to print out each of the numbers in b as a singleton list. (4 points)

List Comprehensions and Tuples

Your repository has the skeleton of a program in listcomp.hs where you will complete the practice tasks below.

Practice Program: Write a list comprehension, and store the result in a, that produces a list of tuples, each of which has two numbers between 1 and 10, inclusive, as each of its first two elements, and the sum of the squares of those as the third. (10 points)

Practice Program: Write a second version of your list comprehension from the previous task, storing the result in b, so the list returned does not have any "duplicate" entries, where a duplicate is defined as two tuples where the only difference is that the first and the second elements are swapped. So if (2,3,13) is in your list, (3,2,13) should not be. (5 points)

Practice Program: Write a function singletons that takes a list as its parameter and returns a list of singleton lists, where each element in the input list becomes the one element within the singleton list at the corresponding position. That is, the list [9,2,3] becomes [[9],[2],[3]]. (5 points)

Submission

Commit and push!

Grading

This assignment will be graded out of 35 points.

Feature

Value Score
preds.hs 5
listpractice.hs print each number 6
listpractice.hs singletons 4
listcomp.hs sum of squares tuple list 10
listcomp.hs restricted list 5
listcomp.hs singletons 5
Total 35