Question

In: Computer Science

Please write code in Scala Write a function which check that numbers in provided list are...

Please write code in Scala

Write a function which check that numbers in provided list are correctly sorted. Please use tail recursion

Solutions

Expert Solution

HELLO

I AM ADDING CODE HERE

PLEASE GO THROUGH IT

def isSorted[T](list: List[T])(implicit ord: Ordering[T]): Boolean = list match {
    case Nil => true // an empty list is sorted
    case x :: Nil => true // a single-element list is sorted
    case x :: xs => ord.lteq(x, xs.head) && isSorted(xs) // if the first two elements are ordered and the rest are sorted, the full list is sorted too
}

val nums: List[Int] = List(1, 2, 3, 4, 5)
isSorted(nums)

OUTPUT


Related Solutions

Please write code in SCALA Thanks in advance Use curried representation and write system which convert...
Please write code in SCALA Thanks in advance Use curried representation and write system which convert pressure units. Input: pressure in atm (atmosphere) Outputs: PSI, bar, torr 1 atm = 14.6956 psi = 760 torr = 101325 Pa = 1.01325 bar.
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
IN SCHEME Write a function subtract which takes a list of numbers as a parameter and...
IN SCHEME Write a function subtract which takes a list of numbers as a parameter and returns a list of the differences. So, a list containing the second minus the first, third minus the second, etc. (subtract '(3 4 10 14 5)) '(1 6 4 -9)
Write a function that will accept a list of numbers and an integer (n). The function...
Write a function that will accept a list of numbers and an integer (n). The function should return a list containing every nth item from the input list, always starting with the first item in the list. The original list should not be modified. For example, if the function is passed the list [8, 3, 19, 26, 32, 12, 3, 7, 21, 16] and the integer 3, it will return the list [8, 26, 3, 16] If the function is...
Write a Python function which finds the smallest even and odd numbers in a given list....
Write a Python function which finds the smallest even and odd numbers in a given list. (Use for loop and if conditions for this problem)
Provided is a list of numbers. For each of the numbers in the list, determine whether...
Provided is a list of numbers. For each of the numbers in the list, determine whether they are even. If the number is even, add True to a new list called is_even. If the number is odd, then add False. num_lst = [3, 20, -1, 9, 10] **Please use Python**
In Scala, what is First-order Function, Recursion, and Immutability? How to write them? Please provide some...
In Scala, what is First-order Function, Recursion, and Immutability? How to write them? Please provide some examples and details to explain it. Also, what will be the benefits of using them when writing codes.
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
can someone please check this code? – Enter Quantity of Numbers Design a program that allows...
can someone please check this code? – Enter Quantity of Numbers Design a program that allows a user to enter any quantity of numbers until a negative number is entered. Then display the highest number and the lowest number. For the programming problem, create the pseudocode and enter it below. Enter pseudocode here start     Declarations           num number           num high             num low              housekeeping()     while number >=0 detailLoop()      endwhile      finish() stop housekeeping( )           output...
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT