Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

Haskell Functions and Compression/Decompression Algorithms

Overview

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.

Project Structure

  • 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 rev function 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.

Script 1 Functions

List and Tuple Operations

  • second1 :: [a] -> a: Returns the second element of a list using head and tail.
  • second2 :: [a] -> a: Returns the second element using the list indexing operator !!.
  • second3 :: [a] -> a: Uses pattern matching to directly return the second element.

Miscellaneous Utility Functions

  • e1 to e4: 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.

Script 2 Functions

XOR and Other Algorithms

  • 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.

Compression and Decompression

Compression Functions

  • 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.

Decompression Functions

  • 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.

How to Run

  1. Prerequisites: Ensure you have a Haskell compiler (e.g., GHC) installed on your machine.

  2. 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.
  3. Example Usage:

    • Run the compress function on a sample string to observe the compression process.
    • Use the decompress function to revert the compressed string back to its original form.

Acknowledgments

This project was developed as part of a coursework assignment at University. Special thanks to the course instructors for their guidance.


T