Created
May 17, 2015 14:45
-
-
Save umairsd/cdcb397941762fe02d05 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Index based list operations using folds | |
| -- | |
| -- Author: Umair Saeed | |
| -- Date: 05/17/2015 | |
| -- | |
| -- insert at an index, implemented via foldr | |
| insertAt :: a -> [a] -> Int -> [a] | |
| insertAt x ys n = foldr insertHelper [] $ zip [0..] ys | |
| where | |
| insertHelper (i,y) acc = if i == n | |
| then x : y : acc | |
| else y : acc | |
| -- delete at an index, implemented via foldr | |
| deleteAt :: [a] -> Int -> [a] | |
| deleteAt ys n = foldr deleteHelper [] $ zip [0..] ys | |
| where | |
| deleteHelper (i,y) acc = if i == n then acc else y : acc | |
| -- update at an index, implemented via foldr | |
| updateAt :: a -> [a] -> Int -> [a] | |
| updateAt x ys n = foldr updateHelper [] $ zip [0..] ys | |
| where | |
| updateHelper (i,y) acc = if i == n then x : acc else y : acc | |
| -- insertAt, implemented via foldl | |
| insertAtL :: a -> [a] -> Int -> [a] | |
| insertAtL x ys n = foldl insertLHelper [] $ zip [0..] ys | |
| where | |
| insertLHelper acc (i,y) = if i == n | |
| then acc ++ [x] ++ [y] | |
| else acc ++ [y] | |
| -- deleteAt, implemented via foldl | |
| deleteAtL :: [a] -> Int -> [a] | |
| deleteAtL ys n = foldl deleteLHelper [] $ zip [0..] ys | |
| where | |
| deleteLHelper acc (i,y) = if i == n then acc else acc ++ [y] | |
| -- updateAt, implemented via foldl | |
| updateAtL :: a -> [a] -> Int -> [a] | |
| updateAtL x ys n = foldl updateLHelper [] $ zip [0..] ys | |
| where | |
| updateLHelper acc (i,y) = if i == n then acc ++ [x] else acc ++ [y] | |
| -- Wrapper to help with printing the results | |
| insertAtTester :: (Show a) => a -> [a] -> Int -> String | |
| insertAtTester x ys n = "Insert " ++ (show x) ++ " at " ++ (show n) ++ ": " ++ show (insertAt x ys n) | |
| deleteAtTester :: (Show a) => [a] -> Int -> String | |
| deleteAtTester ys n = "Delete at " ++ (show n) ++ ": " ++ show (deleteAt ys n) | |
| updateAtTester :: (Show a) => a -> [a] -> Int -> String | |
| updateAtTester x ys n = "Update " ++ (show x) ++ " at " ++ (show n) ++ ": " ++ show (updateAt x ys n) | |
| -- Main function, with some examples to test the functions | |
| main = do | |
| let testStr = "abcdefghi" | |
| putStrLn $ "Test string is: " ++ testStr | |
| putStrLn "---" | |
| putStrLn $ insertAtTester 'X' testStr 0 | |
| putStrLn $ insertAtTester 'X' testStr 2 | |
| putStrLn $ insertAtTester 'X' testStr 8 | |
| putStrLn $ insertAtTester 'X' testStr (-2) | |
| putStrLn $ insertAtTester 'X' testStr 10 | |
| putStrLn "---" | |
| putStrLn $ deleteAtTester testStr 0 | |
| putStrLn $ deleteAtTester testStr 2 | |
| putStrLn $ deleteAtTester testStr 8 | |
| putStrLn $ deleteAtTester testStr (-2) | |
| putStrLn $ deleteAtTester testStr 10 | |
| putStrLn "---" | |
| putStrLn $ updateAtTester 'X' testStr 0 | |
| putStrLn $ updateAtTester 'X' testStr 2 | |
| putStrLn $ updateAtTester 'X' testStr 8 | |
| putStrLn $ updateAtTester 'X' testStr (-2) | |
| putStrLn $ updateAtTester 'X' testStr 10 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment