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 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 |
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 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 |
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 FunctionalLoops { | |
def repeatLoop(command: => Unit)(condition: => Boolean) { | |
command | |
if (condition) { | |
repeatLoop(command)(condition) | |
} else () | |
} | |
def repeatUntilLoop(command: => Unit) = { | |
class RepeatUntilLoopClass(command: => Unit) { |
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 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")), |
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 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 { |
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 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? : " + |
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 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))); |
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)) | |
} |
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 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) |