Skip to content

Instantly share code, notes, and snippets.

View cgopalan's full-sized avatar

Chandrakant Gopalan cgopalan

View GitHub Profile
@cgopalan
cgopalan / scala_quicksort.scala
Created September 2, 2011 18:22
Scala Quicksort
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))
@cgopalan
cgopalan / tailrecursivefactorial.scala
Created September 6, 2011 18:56
Scala Tail Recursive Factorial
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))
}
}
@cgopalan
cgopalan / tailrecursivesum.scala
Created September 8, 2011 17:52
Scala Tail Recursive Sum
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))
@cgopalan
cgopalan / higherorderfunctions.scala
Created September 13, 2011 04:04
Scala product
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))
}
}
@cgopalan
cgopalan / factorial2.scala
Created September 13, 2011 04:27
Scala Factorial in terms of a product function
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))
}
@cgopalan
cgopalan / OperatorOverRange.scala
Created September 13, 2011 04:41
Scala generalized sum/product function
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))
}
}
@cgopalan
cgopalan / cuberoot.scala
Created September 14, 2011 21:16
Scala Cube Root
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)
@cgopalan
cgopalan / IntTreeTest.scala
Created September 23, 2011 19:18
Case Classes and Pattern Matching
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)
@cgopalan
cgopalan / ListSort.scala
Created September 23, 2011 19:20
List Insertion Sort
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]) {
@cgopalan
cgopalan / TailRecursiveListLength.scala
Created September 23, 2011 19:21
Tail recurive version of length
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))
}