Created
September 9, 2014 14:24
-
-
Save arjunguha/13de40e021548c025fd8 to your computer and use it in GitHub Desktop.
CS220
This file contains hidden or 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
import cmpsci220._ | |
sealed trait I | |
case class Z() extends I | |
case class S(prev: I) extends I | |
def intToI(n: Int): I = n match { | |
case 0 => Z() | |
case _ => if (n > 0) { S(intToI(n - 1)) } | |
else { error("cannot represent negative numbers") } | |
} | |
fails("negative test") { | |
intToI(-2) | |
} | |
test("positive test") { | |
assert(intToI(3) == S(S(S(Z())))) | |
} | |
case class Point(x: Double, y: Double) | |
// y = m1.x + b1 | |
// y = m2.x + b2 | |
// m1 * x + b1 = m2 * x + b2 | |
// (m1 - m2) * x = b2 - b1 | |
// x = (b2 - b1) / (m1 - m2) | |
def intersect(m1: Double, b1: Double, m2: Double, b2: Double): Point = { | |
val x = (b2 - b1) / (m1 - m2) | |
val y = m1 * x + b1 | |
Point(x, y) | |
} | |
sealed trait OptionalPoint | |
case class NoPoint() extends OptionalPoint | |
case class SomePoint(pt: Point) extends OptionalPoint | |
def intersect(m1: Double, b1: Double, m2: Double, b2: Double): OptionalPoint = { | |
if (m1 == m2) { | |
NoPoint() | |
} | |
else { | |
val x = (b2 - b1) / (m1 - m2) | |
val y = m1 * x + b1 | |
SomePoint(Point(x, y)) | |
} | |
} | |
def foo(m1: Double, b1: Double, m2: Double, b2: Double): String = | |
intersect(m1, b1, m2, b2) match { | |
case NoPoint() => "no intersection" | |
case SomePoint(Point(x, y)) => s"($x, $y)" | |
} | |
case class Time(h: Int, m: Int, s: Int) | |
sealed trait OptionalAlarm | |
case class NoAlarmSet() extends OptionalAlarm | |
case class AlarmSet(t: Time) extends OptionalAlarm | |
case class AlarmClock(currentTime: Time, alarm: OptionalAlarm) | |
val alarmClock1 = AlarmClock(Time(9, 30, 0), NoAlarmSet()) | |
val alarmClock2 = AlarmClock(Time(9, 30, 0), AlarmSet(Time(9, 31, 0))) | |
////////////////////////// | |
sealed trait Option[A] | |
case class None[A]() extends Option[A] | |
case class Some[A](x: A) extends Option[A] | |
def add(x: Int, y: Int) = x + y | |
def intersect3(m1: Double, b1: Double, m2: Double, b2: Double): Option[Point] = { | |
if (m1 == m2) { | |
None() | |
} | |
else { | |
val x = (b2 - b1) / (m1 - m2) | |
val y = m1 * x + b1 | |
Some(Point(x, y)) | |
} | |
} | |
/////////////////////// | |
sealed trait List[A] | |
case class Empty[A]() extends List[A] | |
case class Cons[A](head: A, tail: List[A]) extends List[A] | |
def addToHead[A](elt: A, lst: List[A]): List[A] = { | |
Cons(elt, lst) | |
} | |
def addToTail[A](elt: A, lst: List[A]): List[A] = lst match { | |
case Empty() => Cons(elt, Empty()) | |
case Cons(head, tail) => Cons(head, addToTail(elt, tail)) | |
} | |
def add1All(lst: List[Int]): List[Int] = lst match { | |
case Empty() => Empty() | |
case Cons(head, tail) => Cons(head + 1, add1All(tail)) | |
} | |
test("add1All test") { | |
assert(add1All(Cons(1, Cons(3, Cons(5, Empty())))) == | |
Cons(2, Cons(4, Cons(6, Empty())))) | |
} | |
val strLst = Cons("hello", Cons("string", Cons("list", Empty()))) | |
def lengthAll(lst: List[String]): List[Int] = lst match { | |
case Empty() => Empty() | |
case Cons(head, tail) => Cons(head.length(), lengthAll(tail)) | |
} | |
test("lengthAll test") { | |
assert(lengthAll(strLst) == Cons(5, Cons(6, Cons(4, Empty())))) | |
} | |
///////////////////// | |
def add1(n: Int): Int = { | |
n + 1 | |
} | |
def add1All(lst: List[Int]): List[Int] = lst match { | |
case Empty() => Empty() | |
case Cons(head, tail) => Cons(add1(head), add1All(tail)) | |
} | |
def strLength(str: String): Int = str.length() | |
def lengthAll(lst: List[String]): List[Int] = lst match { | |
case Empty() => Empty() | |
case Cons(head, tail) => Cons(strLength(head), lengthAll(tail)) | |
} | |
def map(f : String => Int, lst: List[String]): List[Int] = lst match { | |
case Empty() => Empty() | |
case Cons(head, tail) => Cons(f(head), map(f, tail)) | |
} | |
map(strLength, strLst) | |
def strLength2X(str: String): Int = str.length() * 2 | |
map(strLength2X, strLst) | |
def map[A,B](f : A => B, lst: List[A]): List[B] = lst match { | |
case Empty() => Empty() | |
case Cons(head, tail) => Cons(f(head), map(f, tail)) | |
} | |
map[String, Int](strLength, strLst) | |
map[Int, Int](add1, Cons(1, Cons(2, Cons(3, Empty())))) | |
def doubleStr(str: String): String = str + str | |
map[String, String](doubleStr, strLst) | |
def isEven(n: Int): Boolean = n % 2 == 0 | |
def filter[A](f: A => Boolean, lst: List[A]): List[A] = lst match { | |
case Empty() => Empty() | |
case Cons(head, tail) => | |
if (f(head)) { | |
Cons(head, filter(f, tail)) | |
} | |
else { | |
filter(f, tail) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment