Question

In: Computer Science

haskell : write a function that reverse the first three element of a list, but not...

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]

Solutions

Expert Solution

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


Related Solutions

Can someone show me how to write a Haskell function using snd on a list of...
Can someone show me how to write a Haskell function using snd on a list of tuples to create a new list of just the second element in each tuple? Ex. [(‘m’, False), (‘w’, False), (‘n’, True)] would evaluate to [False, False, True].
In C++, Write a function to reverse the nodes in a linked list. You should not...
In C++, Write a function to reverse the nodes in a linked list. You should not create new nodes when you reverse the the linked list. The function prototype:          void reverse(Node*& head); Use the following Node definition: struct Node {    int data;    Node *next; }
Write a function in any functional programming language that will reverse a general list. For example,...
Write a function in any functional programming language that will reverse a general list. For example, if an input is (A (B C (D E)) F), output is (F ((E D) C B) A).  Please note that any built-in/pre-defined function, e.g., reverse, cannot be used in your answer. Please DO NOT hard-code any input values, output values in your code. Please submit a screenshot of where your code got compiled, executed, showing the execution result
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
write a c++ member function that removes the first instance of a specific element in a...
write a c++ member function that removes the first instance of a specific element in a linked list and then return the size of the list after the removal whether it was successful or not.
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a...
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a linked list. After attemtped removal return the SIZE of the linked lost whether or not the removal was successful.
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following behavior: • When x, y, and z all correspond to digit values (i.e., integers between 0 and 9, inclusive), combine x y z returns the integer given by the sequence of digits x y z. (That is, x is treated as the digit in the hundreds place, y is treated as the digit in the tens place, and z is treated as the digit...
Using Haskell, how do you make a function i.e test that will take a list of...
Using Haskell, how do you make a function i.e test that will take a list of integers and add a 5 to the end of that list ex: test [1, 2, 3] will be changed to test [1, 2, 3, 5]
PYTHON: Write a recursive function named linear_search that searches a list to find a given element....
PYTHON: Write a recursive function named linear_search that searches a list to find a given element. If the element is in the list, the function returns the index of the first instance of the element, otherwise it returns -1000. Sample Output >> linear_search(72, [10, 32, 83, 2, 72, 100, 32]) 4 >> linear_search(32, [10, 32, 83, 2, 72, 100, 32]) 1 >> linear_search(0, [10, 32, 83, 2, 72, 100, 32]) -1000 >> linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r',...
reverse_number_in_list(number_list:list)-> list This function will be given a list of numbers your job is to reverse...
reverse_number_in_list(number_list:list)-> list This function will be given a list of numbers your job is to reverse all the numbers in the list and return a list with the reversed numbers. If a number ends with 0 you need to remove all the trailing zeros before reversing the number. An example of reversing numbers with trailing zeros: 10 -> 1, 590 -> 95. None of the numbers in the number_list will be less than 1. Example: number_list = [13, 45, 690,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT