Skip to content

Instantly share code, notes, and snippets.

@pseud0n
Created December 7, 2021 20:31
Show Gist options
  • Save pseud0n/5feb459a2289f374ecf4d637a07b0477 to your computer and use it in GitHub Desktop.
Save pseud0n/5feb459a2289f374ecf4d637a07b0477 to your computer and use it in GitHub Desktop.
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