Function Construct

Starting in version 2.0, users can define their own functions in NML using the function construct. Functions are defined in a similar manner to blocks, as shown below:

 function: function_name;
 inputs: list of comma separated inputs;
 outputs: list of comma separated outputs;
 defs: suite;

Suite represents a set of statements which can be a function call, assignment statement, conditionals or loops. Each statement inside a function definition ends with a newline or carriage-return. Semicolon ";" is used to denote the end of function definition.

Example:

function: lossfunc
  inputs:  y_true ;
  outputs: y_pred ;
  defs:

  y_true = y_true ^ 2
  y_pred = y_true ;

Function calls:

User defined functions are called just like any other function:

lossfunc: [in, out]

Assignment statement:

variable = value

Examples:

c = [[2,3],"hi",[3,4,5]]
y = 2 ^ 2
p = range:[len: [c]]

Conditionals:

The if, elif, else blocks all start and end with parentheses. However, if specified a block should contain at least one suite statement. Nested if statements or even other loop statements inside if, elif, and else blocks are allowed.

Syntax:

if condition:
{
suite
}
elif condition:
{
suite
}
else:
{
suite
}

Suite represents a set of statements which can be a function call, assignment statement, conditionals or loops. The elif and else statements are optional.

Example:

if k==4:
{
k=k+1
}
elif k <= 3:
{
k=k^2
}
else:
{
print:[k]
}

While Loop

The while and else blocks all start and end with parentheses. However, if specified a block should contain at least one suite statement. Nested loops or conditionals are allowed.

Syntax:

while condition:
{
suite
}
else:
{
suite
}

Suite represents a set of statements which can be a function call, assignment statement, conditionals or loops. The else statement is optional.

Example:

while True:
{
print:["Condition Satisfied"]
}
else:
{
print: ["Condition Not Satisfied"]
}

For Loop

Syntax:

for variable in list:
{
suite
}
else:
{
suite
}

Suite represents a set of statements which can be a function call, assignment statement, conditionals or loops. The else statement is optional.

Example:

for a in [1,3,4]:
{
h=h+a
}