This project consists of three primary scripts written in Haskell. These scripts demonstrate the use of Haskell's pattern matching, recursion, and list manipulation features through various small functions. Additionally, the project includes a set of functions for compressing and decompressing strings using a basic run-length encoding algorithm.
-
Script 1 (Functions on Lists and Tuples): Defines several utility functions that manipulate lists and tuples. These functions include selecting elements, applying operations, and simple mathematical operations.
-
Script 2 (Coursework 2): Contains more advanced functions, including implementations of the XOR operation, sum of squares, grid generation, Euclidean algorithm for GCD, and a fast reverse list function. It also includes an implementation of the
revfunction for list reversal. -
Coursework 2 (Compression and Decompression): Implements a basic run-length encoding algorithm with functions for compression and decompression of strings. This includes defining helper functions to break down the process into manageable steps, making the code modular and easy to follow.
second1 :: [a] -> a: Returns the second element of a list usingheadandtail.second2 :: [a] -> a: Returns the second element using the list indexing operator!!.second3 :: [a] -> a: Uses pattern matching to directly return the second element.
e1toe4: Examples of lists, tuples, and functions with basic operations.e5 :: Num a => a -> a: Multiplies a number by 2.e6 :: Int -> Int -> Int: Adds two integers.e7 :: (b,a) -> (a,b): Swaps the elements of a tuple.e8 :: a -> (a,a): Returns a tuple containing two copies of the input.copy,one,first,second,mult: Additional utility functions for various simple operations.
xor1,xor2,xor3: Different implementations of the XOR function using pattern matching,if-then-else, and guards.sumsqr :: Int -> Int: Calculates the sum of squares of integers up to a given number.grid :: Int -> [(Int,Int)]: Generates a grid of coordinate pairs.euclid :: Int -> Int -> Int: Implements the Euclidean algorithm for finding the greatest common divisor (GCD).fastrev,rev: Fast reverse function for lists using an accumulator pattern.
chomp :: String -> String: Returns a string of consecutive identical characters from the start.munch :: String -> String: Limits the string length to 9 characters.runs :: String -> [String]: Splits the string into a list of runs of identical characters.encode :: String -> [(Char,Int)]: Converts a string into a list of character-count pairs.flatten :: [(Char,Int)] -> String: Converts a list of character-count pairs back into a string.compress :: String -> String: Compresses a string using run-length encoding.
decode :: [(Char,Int)] -> String: Converts a list of character-count pairs into a string.expand :: String -> [(Char,Int)]: Converts a compressed string back into character-count pairs.decompress :: String -> String: Decompresses a string encoded with run-length encoding.
-
Prerequisites: Ensure you have a Haskell compiler (e.g., GHC) installed on your machine.
-
Running Scripts:
- Load the scripts into a GHCi session or compile them using GHC.
- Execute the functions defined in the scripts to see their outputs and behavior.
-
Example Usage:
- Run the
compressfunction on a sample string to observe the compression process. - Use the
decompressfunction to revert the compressed string back to its original form.
- Run the
This project was developed as part of a coursework assignment at University. Special thanks to the course instructors for their guidance.
T