Skip to content

Instantly share code, notes, and snippets.

@LucianU
Last active August 9, 2018 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucianU/1be522cba081617e3a37813bdd754812 to your computer and use it in GitHub Desktop.
Save LucianU/1be522cba081617e3a37813bdd754812 to your computer and use it in GitHub Desktop.
fromIndex :: Int -> [a] -> Maybe a
fromIndex 0 (x:xs) =
Just x
fromIndex index [] =
Nothing
fromIndex index (x:xs) =
fromIndex (index - 1) xs
@sshine
Copy link

sshine commented Aug 9, 2018

  1. You don't need the [x] special case. (This doesn't work as a base case, since [] is not caught. Use [] instead.)
  2. Your fromIndex' is only necessary to keep track of currIndex, but you could use index recursively and subtract from it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment