Question

2b Please code in language: Ocaml TODO: Implement a recursive OCaml function ascending : int ->...

2b

Please code in language: Ocaml

TODO: Implement a recursive OCaml function ascending : int -> int list, which accepts an integer n as input (again, assume n ≥ 0), and returns a list of integers from 0 to n in ascending order.

starter code:

let ascending (n : int) : int list = (* your work here *) [ ]

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Below program returns the list of integers within the range a and b, if a > b then list is displayed in descending order. For the given question use a=0 and b=N.

# let range a b =
    let rec aux a b =
      if a > b then [] else a :: aux (a+1) b  in
    if a > b then List.rev (aux b a) else aux a b;;
val range : int -> int -> int list = <fun>

A tail recursive implementation:

# let range a b =
    let rec aux acc high low =
      if high >= low then
        aux (high::acc) (high - 1) low
      else acc
    in if a < b then aux [] b a else List.rev (aux [] a b);;
val range : int -> int -> int list = <fun>

Output

# range 4 9;;
- : int list = [4; 5; 6; 7; 8; 9]
# range 9 4;;
- : int list = [9; 8; 7; 6; 5; 4]
Add a comment
Know the answer?
Add Answer to:
2b Please code in language: Ocaml TODO: Implement a recursive OCaml function ascending : 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
  • 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 *) [ ]

  • 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 (*...

  • In java need help with the TODO sections creating recursive methods public class CountUpDown { /**...

    In java need help with the TODO sections creating recursive methods public class CountUpDown { /** * countUp - a recursive function that counts up from 1 to n * * @param n the integer value to count up to */ private static void countUp(int n) { // TODO PRELAB // IMPLEMENT THIS RECURSIVE METHOD } /** * countDown - a recursive function that counts down from n to 1 * * @param n the integer value to count down...

  • 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...

  • Please write program in C language. 2.Write a recursive int function to return the number of...

    Please write program in C language. 2.Write a recursive int function to return the number of even integers in a linked list. Each node in the list has an integer number (declared as int number) and a pointer to the next node (declared as NODE *next). The function is defined as int count (NODE *); and is called as follows: x = count(head);

  • (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...

    (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search function...

  • Recursive strategy in C // gcc factorial.c -o factorial // ./factorial #include <stdio.h> // TODO: Implement...

    Recursive strategy in C // gcc factorial.c -o factorial // ./factorial #include <stdio.h> // TODO: Implement iterative solution here int factorial(int n){ return 0; }; // TODO: Implement recursive solution here int factorial_rec(int n){ return 0; } int main(){ printf("factorial(10) = %d\n",factorial(10)); printf("factorial_rec(10) = %d\n",factorial_rec(10)); Once you have solved the above, answer and modify your programs for the following. What is a bigger data type you can use to compute larger factorials? Change the 'return type' and input parameters to...

  • if it is possible help me with the code in C language and I could learn...

    if it is possible help me with the code in C language and I could learn it, thanks ! Write a function that computes the integer mean of an array of integers. For example, the integer mean of -1, 4, 2 is (-1 + 4 + 2)/3 = 5/3 = 1, where/des integer division. The function should implement the following specification:/* Computes the integer mean of an array and stores it * where mn references. Returns -1 for erroneous input...

  • C++ language Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ language Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "C" "" "" "a" "d" "e" O Full Screen 1 code.cpp + New #include <string> 2 using namespace std; 3 4- int...

  • Question 10 15 pts In this question, you are asked to implement a recursive function that...

    Question 10 15 pts In this question, you are asked to implement a recursive function that checks whether two increasingly sorted arrays of integers are disjoint or not (two arrays are disjoint iff they share no common elements). Your function needs to follow the following signature: int is Disjoint (int" sorted 1, int size 1, int* sorted 2, int size2) Here is the description of the input and output parameters of is Disjoint: • int" sorted 1: pointer to the...

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