Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • shchen/cs320
  • raveendr/cs320
  • mwojnaro/cs320
3 results
Show changes
Showing
with 382 additions and 0 deletions
object WrongQName
42 + foo.bar + 1
end WrongQName
object Assoc
((1 + 2) - 3);
((x / y) * z);
-(42);
((1 < 2) <= 3);
(true || false);
((1 < 2) && (2 < 3))
end Assoc
object AssocSemicolon
1;
2;
val x: Int(32) =
z;
val y: Int(32) =
w;
u
end AssocSemicolon
object ChainedMatch
0 match {
case x =>
x
} match {
case 42 =>
"Yeah"
}
end ChainedMatch
\ No newline at end of file
object CaseClassDefs
abstract class Foo
case class Bar() extends Foo
case class Bar(v: Int(32)) extends Foo
abstract class Foo
case class Bar(v: Int(32), v: A) extends Foo
end CaseClassDefs
object Empty
end Empty
object ErrorToken1
error("")
end ErrorToken1
object ErrorToken2
(false || error(""))
end ErrorToken2
object FunCalls
def foo(i: Int(32)): Int(32) = {
i
}
def bar(i: Int(32), j: Int(32)): Int(32) = {
(i + j)
}
foo(1);
val foz: Int(32) =
4;
foo(foz);
foo((
val f: Int(32) =
42;
f
));
bar(1, 2);
val baz: Int(32) =
4;
foo(foz, baz);
foo((
val f: Int(32) =
42;
f
), (
val b: Int(32) =
1;
b
))
end FunCalls
\ No newline at end of file
object FunDefs
def foo(): Int(32) = {
()
}
def foo(i: Int(32)): Int(32) = {
42
}
def foo(i: I, j: Boolean, s: String, u: Unit, x: X): Type = {
42
}
end FunDefs
object IfCondition
(if((
val x: Boolean =
true;
x
)) {
1
} else {
2
})
end IfCondition
object L
abstract class List
case class Nil() extends List
case class Cons(v: Int(32), v: List) extends List
def isEmpty(l: List): Boolean = {
l match {
case Nil() =>
true
case _ =>
false
}
}
def length(l: List): Int(32) = {
l match {
case Nil() =>
0
case Cons(_, t) =>
(1 + length(t))
}
}
def head(l: List): Int(32) = {
l match {
case Cons(h, _) =>
h
case Nil() =>
error("head(Nil)")
}
}
def headOption(l: List): O.Option = {
l match {
case Cons(h, _) =>
O.Some(h)
case Nil() =>
O.None()
}
}
def reverse(l: List): List = {
reverseAcc(l, Nil())
}
def reverseAcc(l: List, acc: List): List = {
l match {
case Nil() =>
acc
case Cons(h, t) =>
reverseAcc(t, Cons(h, acc))
}
}
def indexOf(l: List, i: Int(32)): Int(32) = {
l match {
case Nil() =>
-(1)
case Cons(h, t) =>
(if((h == i)) {
0
} else {
(
val rec: Int(32) =
indexOf(t, i);
(if((0 <= rec)) {
(rec + 1)
} else {
-(1)
})
)
})
}
}
def range(from: Int(32), to: Int(32)): List = {
(if((to < from)) {
Nil()
} else {
Cons(from, range((from + 1), to))
})
}
def sum(l: List): Int(32) = {
l match {
case Nil() =>
0
case Cons(h, t) =>
(h + sum(t))
}
}
def concat(l1: List, l2: List): List = {
l1 match {
case Nil() =>
l2
case Cons(h, t) =>
Cons(h, concat(t, l2))
}
}
def contains(l: List, elem: Int(32)): Boolean = {
l match {
case Nil() =>
false
case Cons(h, t) =>
((h == elem) || contains(t, elem))
}
}
abstract class LPair
case class LP(v: List, v: List) extends LPair
def merge(l1: List, l2: List): List = {
l1 match {
case Nil() =>
l2
case Cons(h1, t1) =>
l2 match {
case Nil() =>
l1
case Cons(h2, t2) =>
(if((h1 <= h2)) {
Cons(h1, merge(t1, l2))
} else {
Cons(h2, merge(l1, t2))
})
}
}
}
def split(l: List): LPair = {
l match {
case Cons(h1, Cons(h2, t)) =>
(
val rec: LPair =
split(t);
rec match {
case LP(rec1, rec2) =>
LP(Cons(h1, rec1), Cons(h2, rec2))
}
)
case _ =>
LP(l, Nil())
}
}
def mergeSort(l: List): List = {
l match {
case Nil() =>
l
case Cons(h, Nil()) =>
l
case l =>
split(l) match {
case LP(l1, l2) =>
merge(mergeSort(l1), mergeSort(l2))
}
}
}
def toString(l: List): String = {
l match {
case Nil() =>
"List()"
case more =>
(("List(" ++ toString1(more)) ++ ")")
}
}
def toString1(l: List): String = {
l match {
case Cons(h, Nil()) =>
Std.intToString(h)
case Cons(h, t) =>
((Std.intToString(h) ++ ", ") ++ toString1(t))
}
}
def take(l: List, n: Int(32)): List = {
(if((n <= 0)) {
Nil()
} else {
l match {
case Nil() =>
Nil()
case Cons(h, t) =>
Cons(h, take(t, (n - 1)))
}
})
}
end L
object Literals
1;
();
();
"Hello";
true
end Literals
object IfMatch
(if(true) {
1
} else {
2
}) match {
case 1 =>
true
case 2 =>
false
}
end IfMatch
object NestedMatch
foo match {
case bar =>
baz match {
case 42 =>
()
}
}
end NestedMatch
object Parens
-(!((1 + ((2 * 3) / (
val x: Int(32) =
(
42;
x
);
(((x + z match {
case foo =>
bar
}) - 3) == (
1;
2
))
)))))
end Parens
object Patterns
x match {
case 1 =>
0
case () =>
0
case "" =>
0
case "Hello" =>
0
case true =>
0
case false =>
0
case x =>
0
case Variable =>
0
case _ =>
0
case C() =>
0
case C(1) =>
0
case C(C(1)) =>
0
case C(C(_, "", C(1, "Hello", ()), ()), D(D(_, D(3))), ()) =>
0
}
end Patterns
object Precedence
val v: Int(32) =
(((1 * 2) < x) || (y + 3));
u match {
case foo =>
bar
};
(w == -((if(x) {
y
} else {
z
})))
end Precedence
object QualifiedNames
val x: Faz.boo =
Foo.bar(x match {
case Foo.baz(foo) =>
1
case Foo.baz(Foo.bar(), foo) =>
2
});
42
end QualifiedNames
object Assoc
1 + 2 - 3;
x / y * z;
-42;
1 < 2 <= 3;
true || false;
1 < 2 && 2 < 3
end Assoc