Skip to content

Instantly share code, notes, and snippets.

View cgopalan's full-sized avatar

Chandrakant Gopalan cgopalan

View GitHub Profile
@cgopalan
cgopalan / CompoundOrGate.scala
Created October 6, 2011 21:57
OR Gate as combination of inverter and AND gate
object CompoundOrGate {
type Action = () => Unit
class Wire {
private var sigVal = false
private var actions: List[Action] = List()
def getSignal = sigVal
def setSignal(s: Boolean) =
if (s != sigVal) {
sigVal = s
@cgopalan
cgopalan / SimpleOrGate.scala
Created October 6, 2011 21:49
Simple OR Gate
object SimpleOrGate {
type Action = () => Unit
class Wire {
private var sigVal = false
private var actions: List[Action] = List()
def getSignal = sigVal
def setSignal(s: Boolean) =
if (s != sigVal) {
sigVal = s
@cgopalan
cgopalan / FunctionalLoops.scala
Created October 6, 2011 03:48
RepeatLoop and RepeatLoop-Until
object FunctionalLoops {
def repeatLoop(command: => Unit)(condition: => Boolean) {
command
if (condition) {
repeatLoop(command)(condition)
} else ()
}
def repeatUntilLoop(command: => Unit) = {
class RepeatUntilLoopClass(command: => Unit) {
@cgopalan
cgopalan / ForToMap.scala
Created October 6, 2011 03:37
Translate for to higher-order functions
object ForToMap {
def main(args: Array[String]) {
case class Book(title: String, authors: List[String])
val books: List[Book] = List(
Book("Structure and Interpretation of Computer Programs",
List("Abelson, Harold", "Sussman, Gerald J.")),
Book("Principles of Compiler Design",
List("Aho, Alfred", "Ullman, Jeffrey")),
Book("Programming in Modula2",
List("Wirth, Niklaus")),
@cgopalan
cgopalan / NQueensProblem.scala
Created October 6, 2011 03:26
Function isSafe for NQueens problem
object NQueensProblem {
def queens(n: Int): List[List[Int]] = {
def placeQueens(k: Int): List[List[Int]] =
if (k == 0) List(List())
else for { queens <- placeQueens(k - 1)
column <- List.range(1, n + 1)
if isSafe(column, queens, 1)} yield column :: queens
placeQueens(n)
}
def isSafe(col: Int, queens: List[Int], delta: Int): Boolean = queens match {
@cgopalan
cgopalan / ListFilter.scala
Created October 6, 2011 03:06
Forall and Exists in term of Filter
object ListFilter {
def forall(xs: List[Int], p: Int => Boolean): Boolean =
xs.isEmpty || xs.filter(p) == xs
def exists(xs: List[Int], p: Int => Boolean): Boolean =
!xs.isEmpty && !xs.filter(p).isEmpty
def main(args: Array[String]) {
println("Are members of list (1,2,3,4,5) less than 10? : " +
forall(List(1,2,3,4,5), (x => x < 10)));
println("Is there at least one member of list (1,2,3,4,5) thats greater than 5? : " +
@cgopalan
cgopalan / ListMap.scala
Created October 6, 2011 02:59
Scala Map function
object ListMap {
def squareList(xs: List[Int]): List[Int] = xs match {
case List() => xs
case y :: ys => y * y :: squareList(ys)
}
def squareListMap(xs: List[Int]): List[Int] =
xs map (x => x * x)
def main(args: Array[String]) {
println("squarelist for square of (9,13,21) returns : " + squareList(List(9, 13, 21)));
@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))
}
@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 / 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)