Question

2a Please code in Ocaml: TODO: Implement an OCaml function sum_top_4 : int list -> int...

2a

Please code in Ocaml:

TODO: Implement an OCaml function sum_top_4 : int list -> int list, which takes a list of integers and adds the sum of the top four elements to the head of the list (e.g. the input list [1;1;1;1;3;2] should become the output list [4;1;1;1;1;3;2]).

starter code:

let sum_top_4 (xs : int list) : int list = (* your work here *) [ ]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer :

let sum_top_k   =

fun arg -> let list = [1;1;1;1;3;2] in

let rec sum_top_k k =

if k=1 then list.(0)

else list.(k-1) + sum_top_k (k-1)

in sum_top_k arg ; ;

sum_first_k 4 ;;

( * int - : int = 4 *)

Add a comment
Know the answer?
Add Answer to:
2a Please code in Ocaml: TODO: Implement an OCaml function sum_top_4 : int list -> int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in ocaml language Execute > Share main.ml STDIN 2 (* Problem 9. Write a function that...

    in ocaml language Execute > Share main.ml STDIN 2 (* Problem 9. Write a function that takes 3 an integer list and returns the sum of all elements in the list 1.li Result Socamle - main ..ml File "main.ml", line 9, characters 29-37: Error: This expression has type int option but an expression was expected of type int 4 If the list is empty, then return None. *) 5 let rec sum (xs:int list) : int option = 6 (*...

  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • Implement the function odd_total (xs) that takes a list of list of ints xs and returns...

    Implement the function odd_total (xs) that takes a list of list of ints xs and returns the sum of the odd numbers but skips the numbers on the first row and the last column of xs. You're not allowed to modify xs. You can't use any built-in function or method except int, str, range and len. Example: odd_total ([[1,2,3],[4,5,6], [7,8,9]]) - - > 12

  • F# ONLY SOMEONE SOLVED IN PYTHON JUST NOW. I NEED F SHARP PROGRAMMING THANKS A more...

    F# ONLY SOMEONE SOLVED IN PYTHON JUST NOW. I NEED F SHARP PROGRAMMING THANKS A more efficient version of the function for computing Tetranacci numbers can be defined by following three steps: Implement a function which takes a list of integers and adds the sum of the top four elements to the head of the list (e.g., in F#, 1::1::1::1::[] should become 4::1::1::1::1::[]). Implement a recursive function which accepts an integer n as input (again, assume n >= 0), and...

  • JAVA - Write a recursive function that takes a linked list as input and returns all...

    JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...

  • IN C++: Please write a function called coin_row that takes in a vector (of type int)...

    IN C++: Please write a function called coin_row that takes in a vector (of type int) of coins in order and outputs a coin row solution. ( Find maximum possible sum of elements such that there are no 2 consecutive elements present in the sum.) val set to the value of the optimal solution coin_indices (0-indexed) set to the indices of the coins forming the optimal solution MUST BE DONE WITH DYNAMIC PROGRAMMING. NO RECURSION typedef struct coin_row_solution{ int val;...

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

  • (3) Problem P6.10. Implement a function void remove.duplicates (vector<int> & v) discussed in Problem 6.10 that...

    (3) Problem P6.10. Implement a function void remove.duplicates (vector<int> & v) discussed in Problem 6.10 that removes duplicates from a vector. Write a program that prompts the user to enter a list of integers and then use the above function to remove duplicates. Then display the modified vector. Implement a loop in which the above actions are repeated until the user requests to quit. . Assume that the lists of integers is nonempty Assume that the the user's input is...

  • Implement the selfDot method below, which takes as input an array of integers and returns the...

    Implement the selfDot method below, which takes as input an array of integers and returns the sum of the each of the elements squared. An example main method, as well as corresponding output, is supplied - you cannot this method, and the output when the program is run must conform exactly to the sample output./* sample run: 14 125 */public static void main(String args[]) {int nums1[] = {1, 2, 3}; int nums2[] = {10, 5}; System.out.println(selfDot(nums1));//1*1 + 2*2 + 3*3...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT