Q:

find second element of array through recursion Haskell

I have written the following code which returns the second element of the list.
first :: [Int] -> Int
first []= 0                                                                                                                                        
first [x] =x
first (x:x1:xs) = x1

It is working fine, but I want it to go through recursion, For Example, I keep on recursing the list as soon as it reaches the second number it gives our second number.
Thank you very much.

A:

Good question! The typical way to do this through recursion is to use turn your function into a thin wrapper around a recursive function with an extra argument (typically called go):
first :: [Int] -> Int
first list = go 0 list
  where
    go        1 (x:_)  = x
    go curIndex (x:xs) = go (curIndex+1) xs
    go        _ []     = 0

However, Haskell uses currying, so the definition of first can be slightly simplified to first = go 0.
Another change I would recommend is to use the Maybe type from the Prelude:
data Maybe a = Just a | Nothing

That is, a value of type Maybe Int can be either a Just value containing an Int, or it can be a Nothing value containing no Int. Typically this type is used in situations where a computation can either succeed or fail - such as this function! So it can be changed now to the following:
first :: [Int] -> Maybe Int
first = go 0
  where
    go        1 (x:_)  = Just x     -- returning the second value
    go curIndex (x:xs) = go (curIndex+1) xs
    go        _ []     = Nothing    -- returning Nothing if it doesn't exist

And now we can change the type to make it more generic (and also shorten the name curIndex a bit) to give a final recursive answer. I've also changed the name to second, because that name reflects the function's purpose better (you're getting the second element, after all):
second :: [a] -> Maybe a
second = go 0
  where
    go 1 (x:_)  = Just x
    go i (x:xs) = go (i+1) xs
    go _ []     = Nothing

A:

Not really a « natural » case for recursion but you could write this if you absolutely want recursion...
first :: [Int] -> Int
first []= 0                                                                                                                                        
first [x] =x
first (x:x1:xs) = first [x1]

