Last active
January 4, 2017 07:14
-
-
Save vitaly-pushkar/a5803423e04d432d87f0910aa96f231a to your computer and use it in GitHub Desktop.
Excercise 2.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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