Created
December 7, 2021 20:31
-
-
Save pseud0n/5feb459a2289f374ecf4d637a07b0477 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
data BoundsError = BoundsError { listLength :: Int, attemptedToGet :: Int } deriving Show | |
getItem :: Int -> Int -> [a] -> Either a BoundsError | |
getItem traversedLength tryingToGet [] = Right $ BoundsError traversedLength (tryingToGet + traversedLength) | |
getItem _ 0 (x:_) = Left x | |
getItem traversedLength tryingToGet (x:xs) = getItem (traversedLength + 1) (tryingToGet - 1) xs | |
main :: IO () | |
main = do | |
print $ getItem 0 50 [0..10] | |
print [0..10] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment