Consider what's going on in this function:
second []=[1,2,3,4,5]
second (x:xs)= second xs
You've instructed it to perform recursively. Let's say you call second on any list, like a list of String:
second ["Do", "Re", "Mi"]
I believe the expected behavior is to return "Re", but what will actually happen is it will return [1, 2, 3, 4, 5]. Observe in your pattern match:
second []
second (x:xs)
This is a common method to go through all of the elements of a list. The first pattern will match any empty list passed to the second function, and the second pattern will match any list with one or more elements, than assign the first element the name "x" and the rest of the list the name "xs". In your function, you just call
second xs, which will result in the function being called again and again until there are no elements left in the list, then it matches the first pattern and returns [1, 2, 3, 4, 5]. Simply put, no matter what you input into this function, you will always end up with [1, 2, 3, 4, 5]. :)
If you're trying to get the second element of the list, try this:
second = head . tail
This utilizes Function Composition and Function Currying, which may be a bit advanced for a newbie to Haskell, so let's do it a bit easier:
second [] = error "Cannot send empty list to second"
second (x:xs) = head xs
The
head function just returns the first element of a list, so when you split the list using the x:xs pattern notation, just return the head of xs. You could also implement it like this:
second (_:x:_) = x
second _ = error "Cannot send list with less than 2 elements to second."
This uses strictly pattern matching as well as using the _ wildcard symbol, which essentially means "match anything and ignore this value". It's a value that is not given a name in the function, but nonetheless must be matched.
Tried to explain the best I could, and if there's any trouble with that don't hesitate to ask.