Here's a hint:
reverseFirst2 :: [a] -> [a]
reverseFirst2 (x1:x2:xs) = x2:x1:xs
reverseFirst2 xs = xs
This reverses the order of the first 2 elements of a list
> reverseFirst2 "abcdpqrs"
"bacdpqrs"
Can you fill in the ...
below to do the same thing to the remainder of the list?
reverseEvery2 :: [a] -> [a]
reverseEvery2 (x1:x2:xs) = x2:x1: ...
reverseEvery2 xs = xs
Getting input
To get input one line at a time, read the input one line at a time.
main = do
l <- getLine
print (reverseEvery2 l)
main
The reason you are getting that behavior with getContents
is the input read from getContents
includes the newline characters at the end of the lines. It is essentially running
rev "abcdpqrs\naz\n" =
"badcqpsra\n\nz"