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 311 additions and 0 deletions
test finished
\ No newline at end of file
42
42
2
3
prim
prim
not prim
not prim
test finished
\ No newline at end of file
true
false
true
false
true
false
false
true
false
false
true
true
5! = 120
10! = 3628800
3
8
2
4
1
2
42
\ No newline at end of file
This side effect should only happen once!
yes
42
42
\ No newline at end of file
1
2
1
2
1
2
1
2
1
2
1
2
1
2
Left
Right
SLeft
SRight
CLeft
CRight
\ No newline at end of file
Hello World!
Hello World again!
\ No newline at end of file
9
List(-5, -1, 0, 5, 10)
object Arithmetic
def pow(b: Int(32), e: Int(32)): Int(32) = {
if (e == 0) { 1 }
else {
if (e % 2 == 0) {
val rec: Int(32) = pow(b, e/2);
rec * rec
} else {
b * pow(b, e - 1)
}
}
}
def gcd(a: Int(32), b: Int(32)): Int(32) = {
if (a == 0 || b == 0) {
a + b
} else {
if (a < b) {
gcd(a, b % a)
} else {
gcd(a % b, b)
}
}
}
Std.printInt(pow(0, 10));
Std.printInt(pow(1, 5));
Std.printInt(pow(2, 10));
Std.printInt(pow(3, 3));
Std.printInt(gcd(0, 10));
Std.printInt(gcd(17, 99)); // 1
Std.printInt(gcd(16, 46)); // 2
Std.printInt(gcd(222, 888)) // 222
end Arithmetic
\ No newline at end of file
object BasicArithmetic
def plus(a: Int(32), b: Int(32)): Int(32) = {
a + b
}
def minus(a: Int(32), b: Int(32)): Int(32) = {
a - b
}
def mul(a: Int(32), b: Int(32)): Int(32) = {
a * b
}
def mod(a: Int(32), b: Int(32)): Int(32) = {
a % b
}
def div(a: Int(32), b: Int(32)): Int(32) = {
a / b
}
val a: Int(32) = 1;
val b: Int(32) = 2;
Std.printString(Std.intToString(plus(a, b)));
Std.printString(Std.intToString(mul(plus(a, b), b)));
Std.printString(Std.intToString(minus(mul(plus(a, b), b), b)));
Std.printString(Std.intToString(mul(minus(mul(plus(a, b), b), b), b)));
Std.printString(Std.intToString(div(4, 2)));
Std.printString(Std.intToString(div(3, 2)));
Std.printString(Std.intToString(div(1, 2)));
Std.printString(Std.intToString(div(-1, 2)));
Std.printString(Std.intToString(div(-2, 2)));
Std.printString(Std.intToString(div(-3, 2)));
Std.printString(Std.intToString(mod(5, 2)));
Std.printString(Std.intToString(mod(-5, 2)));
Std.printString("test finished")
end BasicArithmetic
object BasicConditions
def neverTrigger(): Boolean = {
Std.printString("should not happen");
true
}
val c1: Boolean = true || neverTrigger();
val c2: Boolean = false && neverTrigger();
Std.printString("test finished")
end BasicConditions
\ No newline at end of file
object BasicPatternMatching
abstract class Letter
case class A() extends Letter
case class APrim(i: Int(32)) extends Letter
case class B() extends Letter
case class BPrim(i: Int(32)) extends Letter
def isRegularLetter(l: Letter): Boolean = {
l match {
case A() => true
case B() => true
case APrim(_) => false
case BPrim(_) => false
}
}
def getPrimNumber(l: Letter): Int(32) = {
!isRegularLetter(l) match {
case false => 42
case true => l match {
case APrim(i) => i
case BPrim(i) => i
}
}
}
def isPrimLetter(l: Letter): String = {
getPrimNumber(l) match {
case 42 => "not prim"
case _ => "prim"
}
}
val a: Letter = A();
val ap: Letter = APrim(2);
val b: Letter = B();
val bp: Letter = BPrim(3);
Std.printString(Std.intToString(getPrimNumber(a)));
Std.printString(Std.intToString(getPrimNumber(b)));
Std.printString(Std.intToString(getPrimNumber(ap)));
Std.printString(Std.intToString(getPrimNumber(bp)));
Std.printString(isPrimLetter(ap));
Std.printString(isPrimLetter(bp));
Std.printString(isPrimLetter(a));
Std.printString(isPrimLetter(b));
Std.printString("test finished")
end BasicPatternMatching
\ No newline at end of file
object EmptyObject
end EmptyObject
\ No newline at end of file