Written in Go from scratch
There is no need of defining Data type for variable explictly
input :
let a = 10;
puts(a+1)
Output ->
11 null
input:
let b = "String"
puts(b + " is a string")
Output:
String is a string null
input:
let c = [1,2,3]
puts(c[1])
Output:
2 null
input:
puts(1 < 2)
puts(1 > 2)
puts(1 < 1)
puts(1 == 2)
puts(1 != 2)
puts(true == true)
puts(false == true)
puts(true != true)
puts((1 < 2) == false)
Output:
true false false false true true false false false null
input:
let a = 10;
if ( a > 9) {
if (a < 11){
puts(" a is 10")
}
}
else{
puts("a is less than 10");
}
Output:
a is 10 null
input: false and true are seprate datatype
1 == true
Output:
false null
input:
puts(!true)
puts(!false)
puts(!!true)
puts(!!false)</code>
Output:
false true true false null
Every statement is an expression input:
let a = if (true) { 10 }
let b = if (false) { 10 }
let c = if (1) { 10 }
let d = if ( 1 < 2 ) { 10 }
let e = if ( 1 > 2) { 10 } else { 20}
let f = if ( 1 < 2) { 10 } else { 20}
puts(a,b,c,d,e,f)
Output:
10 null 10 10 20 10 null
input:
let add = function(x,y) {
x + y;}
add(5+5, add(5,5));
Output:
20 null
input:
let global = 10;
let testFunction = function(){
puts(global);
let c = 100;
let global = 11;}
testFunction();
puts(global)
puts(c)
Output:
10 10 ERROR: identifier not found: c
input:
let newAdder = function(x){
function(y){
x + y;
};
};
let addTwo = newAdder(2);
addTwo(2);
Output:
4
Array in LAAIT are like list in python
- last(arr) : Give the last element of list
- push(arr,10) : Return a new the array with appending the element at last
- first : Give the first element of list
- rest : Give the rest element of list except first element
input:
let a = [1,2,3,4,5];
puts(a[0], last(a));
let s = "String"
puts(len(s))
let c = push(a, 100)
puts(c);
rest(a)
Output:
1 5 6 [1,2,3,4,5,100] [2,3,4,5]
input:
let c = {"Name" : "Rahul", true: false, 1:20}
puts(c["Name"], c[true], c[1])
Output: Rahul false 20 null
Implementing Fibonacci series input:
let fib = function(a){
if(a == 0){
0;
}
else{
if( a == 1 ) {
1;
}
else{
fib(a - 1) + fib(a -2);
}
}
puts(a);
}
puts(fib(10));
Output:
55 null
Implementing Factorial Program input:
let fact = function(a){
if ( a == 0) { 1}
else { a * fact(a-1) }
}
fact(5)
Output:
120 null