Question

In: Computer Science

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 in the ones place.)

• In all other cases, the function returns -1.

For example, combine 4 2 8 returns 428, combine 0 3 0 returns 30 (because 030 = 30), and combine 4 12 3 returns -1

Solutions

Expert Solution

Code

import java.util.Scanner;

public class Solution {

   public static void main(String[] args) {

       System.out.println(haskell_combine(0, 3, 0));

       System.out.println(haskell_combine(5, 12, 3));

       System.out.println(haskell_combine(4, 2, 8));

       System.out.println(haskell_combine(-4, 2, 8));

   }

   public static int haskell_combine(int x, int y, int z) {

       String t = "" + x + "" + y + "" + z;

      

       if( x >=10 || x<0 || y>=10 || y<0 || z>=10 || z<0)

           return -1;

       else

           return Integer.parseInt(t);

   }

}

Sample output

30

-1

428

-1


Related Solutions

Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int...
C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int x) { return (x * 3); } 2) What is C syntax to declare two variables, one called num of integer type and another called farray which is an array of 10 floating point numbers? 3) Write a C function array_max(int a[], int len) that takes an integer array & its length as its parameters and returns the largest value in the array. 4)...
Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
In Haskell Write a function equal that returns whether two sets are equal. equal :: Set...
In Haskell Write a function equal that returns whether two sets are equal. equal :: Set -> Set -> Bool
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
Consider the following function: int counter( int n) { int s = 0;    for ( int...
Consider the following function: int counter( int n) { int s = 0;    for ( int i = 0; i < n; i++)             for ( int j = i; j > 0; j--)                        s = s + 1;   return s; } Part (a): How many times "s=s+1;" will be executed? Determine a precise count.  Show all your work. Part (b): What is the time complexity of the "counter" function in terms of Big-O notation? Justify and show all your work.
IN C Write a function in the form: void play( int key, int duration) // duration...
IN C Write a function in the form: void play( int key, int duration) // duration units are tenths of a second which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where: n = (duration/10.0)*8000 w = (2π440rkey-49)/8000 r = 21/12 In the main program call your function to play the first three notes of three blind mice.
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT