[SOLVED] CS 431 Programming language concepts Homework 4

30.00 $

Programming resource
Digital learning resource
Category:
✓ Practical programming resource
✓ Suitable for guided study and reference
✓ Tutor guidance available when needed

Description

Rate this product

Homework 4
1 Introduction
For this homework, you will implement some SML functions using pattern
matching.
1. Your solution should be a working ML program in a text le that I can
run on a console using command like use “hwk4.sml”;
2. You may add comments to your program such as (* Question 1 *).
3. Use `let’ expression as necessary for local variables and local functions.
2 Questions
1. Write a function zip that takes two lists and return a list of 2-tuples.
For example, zip ([1, 2, 3], [4, 5]) should return [(1,4), (2,5)].
Note that if one list is longer than the other, the unmatched portion of
the longer list is ignored.
2. Write a function unzip that takes a list of 2-tuples and return a tuple
of two lists. For example, unzip [(1,2), (3,4), (5,6)] should return
([1,3,5], [2,4,6]).
3. Write a function zip3 that takes three lists and return a list of 3-tuples.
For example, zip3 ([1, 2, 3], [4, 5], [6,7,8]) should return [(1,4,6), (2,5,7)].
Note that if one list is longer than the others, the unmatched portion of
the longer list is ignored.
4. Write a function unzip3 that takes a list of 3-tuples and return a tuple of
three lists. For example, unzip3 [(1,2,3), (4,5,6), (7,8,9)] should
return ([1,4,7], [2,5,8], [3,6,9]).
5. Write a function zipWithIndex that takes a list and return a list of 2-
tuples, where each tuple contains an index and a list element. For example,
zipWithIndex [“a”, “b”, “c”] should return [(0, “a”), (1, “b”), (2, “c”)].
1
6. Write a function flatten that takes a list of lists and return a at-
tened list. For example, flatten [[1,2], [3], [4,5,6]] should return
[1,2,3,4,5,6].
7. Write a function flatten2 that takes a list of 2-tuples and return a at-
tened list. For example, flatten2 [(1,2), (3,4), (5,6)] should re-
turn [1,2,3,4,5,6].
3 Submission
Please write your solution in a text le by the name of hwk4.sml and submit it
to the dropbox.
2

Resource details

Understand the Task Before You Use the Resource

Review the requirements, identify the programming concepts involved, study the implementation and test your understanding with your own examples and modifications.