-
Notifications
You must be signed in to change notification settings - Fork 5
Syntax
Prakhar Srivastav edited this page Jan 27, 2016
·
17 revisions
let age : num = 10;
let name : string = "foobar";
let ishappy? : bool = false;
// 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 = List.hd(friends);
val rest = 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 = (x: num) : num => x * x;
val squares = List.map((x : num, y: num): num => x * x + y, [1, 2, 3, 4]);
// finding factorial
def fact (n: int) : int = {
if (n == 0) then {
1;
} else {
n * fact(n-1);
}
}
// summing a list
def sumlist (xs: list num) : num = {
val aux = (total: int, l: list num) => {
if (List.empty?(l))
then { total; }
else { aux (total + List.hd(l), List.tl(l)); }
};
aux(0, xs);
}
JSJS © 2016