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
object QuickSort { | |
def sort(xs: Array[Int]): Array[Int] = { | |
if (xs.length <= 1) xs | |
else { | |
val pivot = xs(xs.length / 2) | |
val lessThanPivot = xs filter (pivot >) | |
val equalToPivot = xs filter (pivot ==) | |
val greaterThanPivot = xs filter (pivot <) | |
println("Pivot for array [%s] is %d".format(xs.deep.mkString(" "), | |
pivot)) |
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
object TailRecursiveFactorial { | |
def factorial(n: Int, product: Int): Int = | |
if (n == 1) product else factorial(n - 1, n * product) | |
def main(args: Array[String]) { | |
println("Factorial of 8 is: " + factorial(8,1)) | |
} | |
} |
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
object FirstClassFunctions { | |
def sum(f: Int => Int)(a: Int, b:Int): Int = { | |
def iter(a: Int, result: Int): Int = | |
if (a > b) result | |
else iter(a + 1, f(a) + result) | |
iter(a,0) | |
} | |
def main(args: Array[String]) { | |
println("Sum is: " + sum(x => x)(1,10)) |
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
object HigherOrderFunctions { | |
def product(f: Int => Int)(a: Int, b:Int): Int = { | |
if (a > b) 1 else f(a) * product(f)(a + 1, b) | |
} | |
def main(args: Array[String]) { | |
println("Product of range of numbers from 3 to 8 is: " + product(x => x)(3,8)) | |
} | |
} |
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
object Factorial { | |
def product(f: Int => Int)(a: Int, b:Int): Int = { | |
if (a > b) 1 else f(a) * product(f)(a + 1, b) | |
} | |
def factorial(n: Int): Int = { | |
product(x => x)(1, n) | |
} | |
def main(args: Array[String]) { | |
println("Factorial of 6 is: " + factorial(6)) | |
} |
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
object OperatorOverRange { | |
def operateOverRange(f: Int => Int)(operate: (Int, Int) => Int)(base: Int)(a: Int, b:Int): Int = | |
if (a > b) base else operate(f(a), operateOverRange(f)(operate)(base)(a + 1, b)) | |
def main(args: Array[String]) { | |
println("Sum of range of numbers from 3 to 8 is: " + operateOverRange(x => x)((x,y) => x + y)(0)(3,8)) | |
println("Product of range of numbers from 3 to 8 is: " + operateOverRange(x => x)((x,y) => x * y)(1)(3,8)) | |
} | |
} |
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
object FixedPoint { | |
var tolerance = 0.0001 | |
def abs(x: Double) = if (x >= 0) x else -x | |
def isCloseEnough(x: Double, y: Double) = abs((x - y) / x) < tolerance | |
def fixedPoint(f: Double => Double)(firstGuess: Double) = { | |
def iterate(guess: Double): Double = { | |
val next = f(guess) | |
println(next) | |
if (isCloseEnough(guess, next)) next | |
else iterate(next) |
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
object IntTreeTest { | |
abstract class IntTree | |
case object EmptyTree extends IntTree | |
case class Node(elem: Int, left: IntTree, right: IntTree) extends IntTree | |
def contains(t: IntTree, v: Int): Boolean = t match { | |
case EmptyTree => false | |
case Node(e, l, r) => | |
if (v < e) contains(l, v) | |
else if (v > e) contains(r, v) |
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
object ListSort { | |
def isort(xs: List[Int]): List[Int] = | |
if (xs.isEmpty) Nil | |
else insert(xs.head, isort(xs.tail)) | |
def insert(head: Int, xs: List[Int]): List[Int] = | |
if (xs.isEmpty) List(head) | |
else if (head > xs.head) xs.head :: insert(head, xs.tail) | |
else head :: xs | |
def main(args: Array[String]) { |
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
object TailRecursiveListLength { | |
def trlength(l: List[Int], counter: Int): Int = l match { | |
case Nil => counter | |
case x :: xs => trlength(xs, counter + 1) | |
} | |
def main(args: Array[String]) { | |
println("Length of list () is: " + trlength(List(), 0)) | |
println("Length of list (1,6,2,5,8,93) is: " + trlength(List(1,6,2,5,8,93), 0)) | |
} |
OlderNewer