-
Notifications
You must be signed in to change notification settings - Fork 5
Syntax
Ayush Jain edited this page Feb 26, 2016
·
17 revisions
num
bool
string
unit
val age : num = 10;
val name : string = "foobar";
val ishappy? : bool = false;
val dontcare : unit = console.log("hello world");
// globals
==, !=, >=, <=, >, <
// num
+ , - , / , * , %,
// string
^ (concat)
// bool
&&, ||, !,
val friends : list string = ["foo", "bar", "baz"];
val nums : list num = [1, 2, 3, 4];
val lists_of_friends : list list string = [["foo"], ["bar"], ["baz"]];
val head : string = List.hd(friends);
val rest : list string = List.tl(friends);
val passwords : <string: num> = {
"foo" : 1245,
"bar" : 5567,
"baz" : 5533
};
val students : <string: <string: num>> = {
"foo": { "marks": 97 },
"bar": { "marks": 23 },
"baz": { "marks": 47 }
};
val foo = Map.get(passwords, "foo");
val foo = Map.set(passwords, "foo", 12345);
val iseven? = if (x % 2 == 0) then {true;} else {false;}
// simple expression
def square (x: num) : num {
x * x;
}
// function multi-line
def cube (x: num): num {
val sq = square(x);
x * sq;
}
// equivalent to def sq (x: num) : num = x * x;
val sq : (num) -> num = (x: num) : num => x * x;
// defining map
def map (f: (T) -> U, l: list T): list U {
if (List.empty?(l))
then { []; }
else {
val res = f(List.hd(l));
List.cons(res, map(f, List.tl(l)));
}
}
// applying map
val squares : list num = map((x: num) : num => x * x, [1, 2, 3, 4]);
// finding factorial
def fact (n: num) : num {
if (n == 0) then {
1;
} else {
n * fact(n-1);
}
}
// summing a list
def sumlist (xs: list num) : num {
val aux = (total: num, l: list num) => {
if (List.empty?(l))
then { total; }
else { aux (total + List.hd(l), List.tl(l)); }
};
aux(0, xs);
}
// printing a list
def printlist (xs: list string) : unit {
List.iter((x: string) : unit => { println(x); } , xs);
}
JSJS © 2016