Question

Write a function (int_to_whole) in OCaml that converts an integer into a whole number if one...

Write a function (int_to_whole) in OCaml that converts an integer into a whole number if one exists (a whole number is 1, 2, 3, ...). Use an option type because not all integer inputs can be converted.

type whole = One | Succ of whole;;

let int_to_whole (x:int) : whole option = ...

Homework Answers

Answer #1

type whole = One | Succ of whole;;

let rec int_to_whole (x:int) : (whole option) =

if x < 0 then None else

match x with

| 0 -> None

| _ -> let y = int_to_whole (x+1) in

match y with

| None -> None

| Some z -> Some (Succ z);;

However, this will lead to stack overflow for large x. You can resolve this problem by making it tail-recursive:

type whole = Zero | Succ of whole

let int_to_whole (x:int) : (whole option) =
if x < 0 then None else
let rec int_to_whole' (x:int) (accum:whole) : (whole option) =
match x with
| 0 -> Some accum
| _ -> int_to_whole' (x+1) (Succ accum)
in int_to_whole' x Zero;;

This OCaml code are correct .

So please any queries please tell here .

Thank you.

Know the answer?
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
Similar Questions
Write an OCaml function named is_square that takes as input an integer, say n, (which may...
Write an OCaml function named is_square that takes as input an integer, say n, (which may be positive or negative) and returns true if that number is square. That is, it returns true if there exists an an integer, say m, if m * m = n. Next, write a function named count_squares that calls count from above to determine the number of squares in a list of integers. For example, count_squares [2;3;5] = 0 count_squares [1;2;3;4;5;6;7;8;9] = 3
Write a function called "isPrime()” that checks if an integer number is prime or not. The...
Write a function called "isPrime()” that checks if an integer number is prime or not. The function returns integer 1 if the number is prime, or integer 0 if the number is not prime. Write a simple test program in main() to show that the function works properly.in C programming language.
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them.         [0.5 mark] (BY USING C PROGRAM)
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
In R- Studio : Write a function that takes as an input a positive integer and...
In R- Studio : Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Write a recursive function, do not use any of the loop commands in your code.
Write a cencrypt() function that takes a arbitrary length string and an integer as inputs and...
Write a cencrypt() function that takes a arbitrary length string and an integer as inputs and returns a string as output. The cencrypt() function you write in lab today needs to work on strings that contain upper-case letters, lower-case letters, or both. The output string should be all upper-case letters. (The Romans of Caesar's time used only what today we call upper-case letters.) So a call to cencrypt("Caesar", 1) should return (return not print) the string “DBFTBS” and a call...
using MATLAB Write a function which will take two inputs: an array ? and an integer...
using MATLAB Write a function which will take two inputs: an array ? and an integer ?. Here, 1 <= ? < ?????h(?). The objective is to find a contiguous subarray of length ? with the largest sum, within the array ?. The output of the function will be that largest sum. For example, x = [1 2 -1 3], and k =2. Then there are 3 possible contiguous subarrays. 1st subarray : [ 1 2], sum = 3 2nd...
For C++: a) Write a function is_prime that takes a positive integer X and returns 1...
For C++: a) Write a function is_prime that takes a positive integer X and returns 1 if X is a prime number, or 1 if X is not a prime number. b) write a program that takes a positive integer N and prints all prime numbers from 2 to N by calling your function is_prime from part a.
10. Write a function in HASKELL howManyBelowAverage that returns how many of three integer inputs are...
10. Write a function in HASKELL howManyBelowAverage that returns how many of three integer inputs are below its average value. (Hint: utilize the averageThree function.) howManyBelowAverage :: Integer -> Integer -> Integer -> Integer averageThree :: Integer -> Integer -> Integer -> Float AverageThree x y z = fromInteger(x+y+z) / 3.0 Can someone please help me solve the howManyBelowAverage function?
An integer number is said to be a perfect number if it is equal to the...
An integer number is said to be a perfect number if it is equal to the sum of its factors (divisors), including 1 (but not the number itself). For example, 6 is a perfect number because 6 = 3+2+1. Write a method called isPerfect that returns true if the input integer number is a perfect number and false otherwise. Then, call this method in the main method that determines and prints all the perfect numbers between 2 and 2000. WRITE...