arrays - How do I get a list item by index in elm? -
I've got a list, and now I want nth item in Haskell I Elm is not equal to it. You can of course apply it to yourself (Note: this is not a "total" function, so it makes an exception when the index is out of range). Using a data type, defining the total work would be a better way. !! , but I can not find the elm type of that.
infixl 9 !! (!!): [A] - & gt; Int - & gt; An xs !! n = head (drop n x)
infixl 9 !! (!!): [A] - & gt; Int - & gt; Maybe an xs !! N = if | N & lt; 0 - & gt; Nothing. Otherwise - & gt; Case (Xs, N) of ([], _) - & gt; Nothing (X :: Xs, 0) - & gt; Just X (_ :: xs, n) - & gt; Xs !! (N-1)
Comments
Post a Comment