Skip to content

Commit 44f8151

Browse files
chase-gegonSchiele
authored andcommitted
Adding Scala examples for Chapter 3 (egonSchiele#28)
* Add files via upload * Update 01_countdown.scala * Update 02_greet.scala * Update 02_greet.scala
1 parent 495a648 commit 44f8151

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

03_recursion/scala/01_countdown.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def countdown(i: Int): Int = {
2+
println(i)
3+
//base case
4+
if(i <= 0) return i
5+
//recursive case
6+
else countdown(i-1)
7+
}
8+
countdown(5)

03_recursion/scala/02_greet.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def greet2(name: String): Unit = {
2+
println("how are you, " + name + "?")
3+
}
4+
5+
def bye(): Unit = {
6+
println("ok bye!")
7+
}
8+
9+
def greet(name: String): Unit = {
10+
println("hello, " + name + "!")
11+
greet2(name)
12+
println("getting ready to say bye...")
13+
bye()
14+
}
15+
16+
greet("adit")

03_recursion/scala/03_factorial.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def fact(x: Int): Int = {
2+
if(x == 1) 1
3+
else x * fact(x-1)
4+
}
5+
println(fact(5))

0 commit comments

Comments
 (0)