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++
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
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.  
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and...
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and "end" (an int). It should return the sum (total) of all of the integers between (and including) "start" and "end". If "end" is less than "start", the function should return -1 instead. e.g. if you give the function a start of 10 and an end of 15, it should return 75 (i.e. 10+11+12+13+14+15)
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int...
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int *q ) that takes two pointers to integer variables and exchanges the values in these variables. Part 1.2 Write a function whose prototype is char lastChar ( /*in*/ const char *str ) that takes a nonempty C-String as parameter and returns the last character in the string. For example the call lastChar ("srjc") will return the character c. Part 1.3 Define an array of...
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int...
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int num) This function should display the multiplication table for values from 1...num. For example, if the function is passed 10 when it is called, it should display the following: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT