Question
List is an important data structure in the functional programming language Haskell . A list may contain redundant items that appear multiple times consecutively: [7,7,7,8,8,8,8,8,7,7]
List is an important data structure in the functional programming language Haskell.
A list may contain redundant items that appear multiple times consecutively:
[7,7,7,8,8,8,8,8,7,7]
In this question, we wish to compress such list into the following compressed list:
[[7,3],[8,5],[7,2]]
Observe that a compressed list is simply a list of [VALUE,TIMES] lists, indicating that the value VALUE appears consecutively in the original list for TIMES times. For example, [7,3] indicates that 7 appears in the original list for 3 times consecutively.
2(a) [20 marks]
Given a compressed list CL, we want to decompress it into its original list NL.
Example: decompress [[7,3],[8,5],[7,2]] into [7,7,7,8,8,8,8,8,7,7]
To solve this problem, we consider two functions:
A function Expand for converting a sublist into a list of consecutive items.
Example: for the input [7,3], the output is [7,7,7]
A function ScanD for scanning the compressed list CL, calling Expand, and appending the lists obtained from Expand
Part i: | Write recursive equations for Expand and ScanD. State the base case(s) clearly. |
|
Part ii: | Write the above functions in the Haskell language. |
|
|
|
|
2(b) |
| [20 marks] |
Given an original list NL, we want to compress it into a compressed list CL.
Example: compress [7,7,7,8,8,8,8,8,7,7] into [[7,3],[8,5],[7,2]]
To solve this problem, we consider three functions:
| A function Count for counting the number of consecutive items at a lists head Example: for the input [7,7,7,8,...], the output is the integer 3 |
| A function Shrink for removing consecutive items at a lists head Example: for the input [7,7,7,8,...], the output is the list [8,...] |
| A function ScanC for scanning the original list NL, calling Count and Shrink, and creating compressed sublists. |
Question 2 Functional Programming List is an important data structure in the functional programming language Haskell A list may contain redundant items that appear multiple times consecutively [7,7,7, 8,8,8,8,8,7,7] In this question, we wish to compress such list into the following compressed list Observe that a compressed list is simply a list of [VALUE, TIMES] lists, indicating that the value VALUE appears consecutively in the original list for TIMES times. For example, [7,3] indicates that 7 appears in the original list for 3 times consecutively 2(a) Given a compressed list CL, we want to decompress it into its original list NL. Example: decompress [[7,3], [8,5], [7,2]] into [7,7,7,8,8,8,8,8,7,7 [20 marks] To solve this problem, we consider two functions A function Expand for converting a sublist into a list of consecutive items. Example: for the input [7,3], the output is [7,7,7] A function ScanD for scanning the compressed list CL, calling Expand, and appending the lists obtained from Expand Write recursive equations for Expand and Scan State the base case(s) clearly Write the above functions in the Haskell language Part i Part ii: [20 marks] 2(b) Given an original list NL, we want to compress it into a compressed list CL. Example: compress [7,7,7,8,8,8,8,8,7,7] into[7,3, [8,5], (7,2]) To solve this problem, we consider three functions A function Count for counting the number of consecutive items at a list's head Example: for the input [7,7,7,8,...1, the output is the integer 3 A function Shrink for removing consecutive items at a list's head Example: for the input [7,7,7,8,...1, the output is the list [8,.. .] A function ScanC for scanning the original list NL, calling Count and Shrink, and creating compressed sublists Part 1: Write recursive equations for Count, Shrink, and Scanc. State the base case(s) clearly Write the above functions in the Haskell language Question 2 Functional Programming List is an important data structure in the functional programming language Haskell A list may contain redundant items that appear multiple times consecutively [7,7,7, 8,8,8,8,8,7,7] In this question, we wish to compress such list into the following compressed list Observe that a compressed list is simply a list of [VALUE, TIMES] lists, indicating that the value VALUE appears consecutively in the original list for TIMES times. For example, [7,3] indicates that 7 appears in the original list for 3 times consecutively 2(a) Given a compressed list CL, we want to decompress it into its original list NL. Example: decompress [[7,3], [8,5], [7,2]] into [7,7,7,8,8,8,8,8,7,7 [20 marks] To solve this problem, we consider two functions A function Expand for converting a sublist into a list of consecutive items. Example: for the input [7,3], the output is [7,7,7] A function ScanD for scanning the compressed list CL, calling Expand, and appending the lists obtained from Expand Write recursive equations for Expand and Scan State the base case(s) clearly Write the above functions in the Haskell language Part i Part ii: [20 marks] 2(b) Given an original list NL, we want to compress it into a compressed list CL. Example: compress [7,7,7,8,8,8,8,8,7,7] into[7,3, [8,5], (7,2]) To solve this problem, we consider three functions A function Count for counting the number of consecutive items at a list's head Example: for the input [7,7,7,8,...1, the output is the integer 3 A function Shrink for removing consecutive items at a list's head Example: for the input [7,7,7,8,...1, the output is the list [8,.. .] A function ScanC for scanning the original list NL, calling Count and Shrink, and creating compressed sublists Part 1: Write recursive equations for Count, Shrink, and Scanc. State the base case(s) clearly Write the above functions in the Haskell language
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started