Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Recursive Thinking in Haskell: Practical Examples
Brain Dump
Sep 9, 2024
130 views
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