In: Computer Science
haskell :
write a function that reverse the first three element of a list,
but not the rest.
example [1,2,3,4,5,6] == [3,2,1,4,5,6]
code:
reverseFirstThree :: [int] -> [int] --function declaration
--if list is empty return emplty list
reverseFirstThree [] = []
--if list contains only one element return same
reverseFirstThree [x] = [x]
--if list contains only one element return same
reverseFirstThree [x,y] = [x,y]
--return x,y,z as z,y,x and unchange the other list elements
reverseFirstThree (x:y:z:xs) = z: y: x: xs
main = do
putStrLn "The addition of the two numbers is:"
print(reverseFirstThree [1,2,3,4,5,6]) --calling a function