Implement a function called runLengthEncode that performs run-length encoding on a list of elements. Run-length encoding is a simple form of data compression that replaces consecutive data elements with a single data value and count.
For example:
Input: "AABBBCCCC"
Output: [(2,'A'), (3,'B'), (4,'C')]
Coming from javascript background my thought process is:
convert the string to a list
create a set, to count the occurance of a char
iterate over the string list
if the currentChar is already in the set, set[currentChar] ++
else set it to 1
Convert the set which has {char: count} i.e {A:2, B: 3, C:4} into a desired output
Now, we also know that in Haskell, we can do following:
Function pattern matching
count & char can be passed down as parameter
since our problem can be broken down into subproblem, we know we can use recursion to solve while problem
defining function type
runLengthEncode :: String -> [(Int, Char)]
base case:
runLengthEncode "" = []
function body:
Convert a string input into a list, leveraging pattern matching.
given "AABBBCCCC"
x = A, xs = "ABBBCCCC"
count of x i,e A. = 1, while starting
runLengthEncode (x:xs) = encode 1 x xs
Next we will write out helper encode function:
since we are leveraging recursion, we will define a base case.
Base case: When we are done processing the last element in the string list i.e C and the remaining list is empty:
we will just return (count, char) if the remaining list is empty
Recursive case: when the we have elements in the list
We will account for if two char matches update count
else we will move forward adding the current char & count to the list.
runLengthEncode (x:xs) = encode 1 x xs
where
encode count char [] = [(count:char)]
encode count char (y:ys)
| char == y = encode (count + 1) char ys
| otherwise = (count :char): encode 1 y ys
when we run this code:
main :: IO()
main = print(runLengthEncode "AABBBCCCC")
-- result -- [(2,'A'),(3,'B'),(4,'C')]
visually this is what happening:
2. Problem runLengthDecode
Implement a function called runLengthDecode that decodes a run-length encoded list back into its original string.
For example:
Input: [(2,'A'), (3,'B'), (4,'C')]
Output: "AABBBCCCC"
We need to learn a replicate
replicate Int -> a -> [a]
replicate 3 'A' -- AAA
replicate 5 0 -- 00000
List Compression: Implement a function that compresses a list by replacing consecutive duplicates with a single copy. This is similar to run-length encoding but without counting.
It stops as soon as it encounters 3, which is not less than 3.
The rest of the list (including 1 and 2 later) is kept
dropWhile even [2, 4, 6, 7, 8, 9] returns [7,8,9]
it drops elements while they are even
it stops as soon as it encounter 7, which is not event
The rest of the list (including 8, which is even) is kept
dropWhile isUpper "ABcdeFG" return "cdeFG"
It drops AB while they are uppercase
It stops as soon as it encounter 'c'
The rest Of the string Uppercase is kept
The key point to understand is that dropWhile only operates on the beginning of the list or string. It stops as soon as it encounters an element that doesn't satisfy the condition, and then it returns the rest of the list or string from that point on, regardless of whether later elements would satisfy the condition.
1. Problem runLengthEncode
Implement a function called runLengthEncode that performs run-length encoding on a list of elements. Run-length encoding is a simple form of data compression that replaces consecutive data elements with a single data value and count.
For example:
Coming from javascript background my thought process is:
Now, we also know that in Haskell, we can do following:
defining function type
base case:
function body:
when we run this code:
visually this is what happening:
2. Problem runLengthDecode
Implement a function called runLengthDecode that decodes a run-length encoded list back into its original string.
For example:
We need to learn a replicate
let's now work on this problem 2.
3. Palindrome Checker
Palindrome Checker: Implement a function that checks if a given string is a palindrome. This will help you practice string manipulation and recursion.
4. List Compression:
List Compression: Implement a function that compresses a list by replacing consecutive duplicates with a single copy. This is similar to run-length encoding but without counting.
dropWhile:
The key point to understand is that dropWhile only operates on the beginning of the list or string. It stops as soon as it encounters an element that doesn't satisfy the condition, and then it returns the rest of the list or string from that point on, regardless of whether later elements would satisfy the condition.
Example usage:
5. Fibonacci Sequence:
Implement a function to generate the fibonacci sequence up n terms.
#Haskell #computerScience #practiceSyntax