Skip to content

Instantly share code, notes, and snippets.

@vitaly-pushkar
Last active January 4, 2017 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitaly-pushkar/a5803423e04d432d87f0910aa96f231a to your computer and use it in GitHub Desktop.
Save vitaly-pushkar/a5803423e04d432d87f0910aa96f231a to your computer and use it in GitHub Desktop.
Excercise 2.2
// implement isSorted, which checks whether an Array[A] is sorted according to a given comparison function:
// def isSorted[A] (as: Array[A], ordered: (A, A) => Boolean): Boolean
object Test {
def isSorted[A](as: Array[A], ordered: (A, A) => Boolean): Boolean = {
def loop(n: Int): Boolean =
if (n+1 >= as.length) true
else if (!ordered(as(n), as(n+1))) false
else loop(n+1)
loop(0)
}
def ordered[A](a: A, b: A): Boolean = {
a < b
}
def main(args: Array[String]): Unit = {
val result = isSorted(Array(1,2,3,5,4), ordered)
println(result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment