Hellow World

Sorry, its a custom for our programmer or coder or any geeks to start learning with Hellow world. So here it is. See and try to understand and at the right moment - forget it ;) !


In [23]:
object HelloWorld {
    def main(args: Array[String]) {
        println("Hello World")
    }
}


Out[23]:
defined object HelloWorld

Save this above code as 'hello.scala' and run like scala hello.scala.

quick start

my fav part :)

lets learn by example. we shall explore values, variables and some crazy def variables. We shall also see what are Arrays look like and then we will define a list of numbers(range in python).


In [25]:
// short form
println("HelloWorld")


HelloWorld

In [28]:
// val -> value; these does not change with time. Like our mentor, teachers -> they help us guide
val a = 12
a


Out[28]:
a: Int = 12
res27_1: Int = 12

In [31]:
// var -> variable; these are like blocks that help build and do wonderful stuff -> they help us build
var names = 1 to 5

println(names)


Range(1, 2, 3, 4, 5)
Out[31]:
names: Range.Inclusive = Range(1, 2, 3, 4, 5)

In [32]:
// for loop, parsing through each variable
for(each <- names) {
  println(each)
}


1
2
3
4
5

In [37]:
var names = Array('a', 'b', 'c')

// for loop, parsing through each variable
for(each <- names) {
  println("variable item - " + each)
}


variable item - a
variable item - b
variable item - c
Out[37]:
names: Array[Char] = Array('a', 'b', 'c')

In [18]:
var x=30
x


Out[18]:
x: Int = 30
res17_1: Int = 30

In [4]:
var x=30l
x


Out[4]:
x: Long = 30L
res3_1: Long = 30L

In [38]:
x += 112
x


Out[38]:
res37_1: Long = 254L

In [39]:
x % 12; x / 12


Out[39]:
res38_0: Long = 2L
res38_1: Long = 21L

In [40]:
x == 12; x < 12; x <= 12; x != 12; x > 13


Out[40]:
res39_0: Boolean = false
res39_1: Boolean = false
res39_2: Boolean = false
res39_3: Boolean = true
res39_4: Boolean = true

In [41]:
x << 1; x >> 2


Out[41]:
res40_0: Long = 508L
res40_1: Long = 63L

In [44]:
256 << 1; 256 >> 2


Out[44]:
res43_0: Int = 512
res43_1: Int = 64

In [45]:
/*
Well we shall know !
*/

"hello " + "world"


Out[45]:
res44: String = "hello world"

variables - functions


In [46]:
def sam = 12


Out[46]:
defined function sam

In [47]:
sam * 12


Out[47]:
res46: Int = 144

Function


In [48]:
def square(x: Int) = x * x


Out[48]:
defined function square

In [49]:
square(12)


Out[49]:
res48: Int = 144

In [50]:
square(121 + 12)


Out[50]:
res49: Int = 17689

In [51]:
square(12); square(211);


Out[51]:
res50_0: Int = 144
res50_1: Int = 44521

In [56]:
square(1) + 2


Out[56]:
res55: Int = 3

In [59]:
def sumOfSquares(x:Int, y:Int) = square(x) + square(x)


Out[59]:
defined function sumOfSquares

In [60]:
sumOfSquares(12, 12)


Out[60]:
res59: Int = 288

conditional experessions


In [61]:
def abs(x: Double) = if (x > 0) x else -x


Out[61]:
defined function abs

In [63]:
abs(12); abs(-12)


Out[63]:
res62_0: Double = 12.0
res62_1: Double = 12.0

In [64]:
def nand(x: Boolean) = x


Out[64]:
defined function nand

In [65]:
nand(true)


Out[65]:
res64: Boolean = true

In [68]:
def and(x: Boolean, y: Boolean)= (x && y)

def nand(x: Boolean, y: Boolean)= !(x && y)


Out[68]:
defined function and
defined function nand

In [69]:
and(true, true); and(true, false); and(false, true); and(false, false);


Out[69]:
res68_0: Boolean = true
res68_1: Boolean = false
res68_2: Boolean = false
res68_3: Boolean = false

In [70]:
nand(true, true); nand(true, false); nand(false, true); nand(false, false);


Out[70]:
res69_0: Boolean = false
res69_1: Boolean = true
res69_2: Boolean = true
res69_3: Boolean = true

In [71]:
def or(x: Boolean, y: Boolean) = x || y
def nor(x: Boolean, y: Boolean) = !(or(x, y))


Out[71]:
defined function or
defined function nor

In [76]:
var mybools = Array(true, false)
mybools


Out[76]:
mybools: Array[Boolean] = Array(true, false)
res75_1: Array[Boolean] = Array(true, false)

In [77]:
for(x <- mybools){
    for(y <- mybools){
        println(x, y, and(x, y), or(x, y));
}
}


(true,true,true,true)
(true,false,false,true)
(false,true,false,true)
(false,false,false,false)

In [84]:
var array_games = 1 to 5

for (each <- array_games) {
    println("variable -> " +  each)
}


variable -> 1
variable -> 2
variable -> 3
variable -> 4
variable -> 5
Out[84]:
array_games: Range.Inclusive = Range(1, 2, 3, 4, 5)

In [85]:
def f(x: Int) = x + 1


Out[85]:
defined function f

In [86]:
f(12)


Out[86]:
res85: Int = 13

In [ ]: