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 Python function that will return the index of an element in a list.1- The...
Write a Python function that will return the index of an element in a list.1- The function will receive a list of at least 5 numbers as a single argument when the function is called. 2. The function will ask the user to input a number and will find and return the index of that number in the list.3. If the number is not an element of the list, the function returns the string "unknown."
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',...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT