Example
Kitchen sink
Variables, functions, scopes, conditions, arrays, loops, recursion-style countdown, and parsing — one program.
Save this as a .alana file, then alana filename.alana. And start coding cute.
define num base is 10;
define decimal multiplier is 2.5;
define str language is "Alana";
define schar symbol is 'A';
define boolean active is true;
define decimal lastReportValue is new;
define num processCount is 0;
define num[] history is [];
define func calculate(num x, num y);
define num result is x + y;
if not result lessthan 20;
return result;
elseif result sameas 15;
return result + 5;
else;
return 0;
end;
end;
define func process(num value);
define num bonus is 15;
define num calculated is call calculate(value, bonus);
redefine processCount is processCount + 1;
if active and calculated greaterthan 20;
define num extra is new;
redefine extra is calculated + 10;
redefine calculated is extra;
end;
push history with calculated;
return calculated;
end;
define func countdown(num value);
for(define num i is value, i greaterthan 0, redefine i is i - 1);
showln i;
end;
return 0;
end;
define func showHistory(num[] values);
show "History contains ";
show length values;
showln " values:";
forevery(define num value within values);
showln value;
end;
end;
define func report(str name, num value);
define num processed is call process(value);
define decimal finalValue is new;
redefine finalValue is processed times multiplier;
redefine lastReportValue is finalValue;
if not active or finalValue lessthan 50;
showln "Processing inactive or too small";
else;
showln name;
showln finalValue;
showln language;
showln symbol;
end;
return finalValue;
end;
show "Enter your name: ";
define str userName is readln;
show "Hello ";
showln userName;
showln;
define num userValue is new;
define boolean numberParsed is false;
while not numberParsed;
show "Enter a number: ";
tryparsenum(readln, userValue, numberParsed);
if not numberParsed;
showln "Invalid number, please try again.";
end;
end;
show "Parsed value: ";
showln userValue;
showln;
define num result is call calculate(base, userValue);
define decimal reportResult is call report("Calculation result:", result);
show "Calculation result: ";
showln result;
show "Report result: ";
showln reportResult;
showln;
showln "Countdown:";
call countdown(5);
showln;
showln "Processing 50:";
showln call process(50);
showln;
show "Last report value: ";
showln lastReportValue;
show "Process count: ";
showln processCount;
showln;
showln "History before modifications:";
call showHistory(history);
showln;
if length history greaterthan 0;
redefine history[0] is 100;
end;
push history with 200;
showln "History after redefine and push:";
call showHistory(history);
pop history;
showln "History after pop:";
call showHistory(history);
showln;
define str processCountText is parsestr(processCount);
show "Process count as string: ";
showln processCountText;
showln;
showln "=== TryParse examples ===";
define decimal parsedDecimal is new;
define boolean decimalParsed is false;
tryparsedecimal("25.75", parsedDecimal, decimalParsed);
if decimalParsed;
show "Parsed decimal: ";
showln parsedDecimal;
else;
showln "Decimal parsing failed";
end;
define boolean parsedBoolean is false;
define boolean booleanParsed is false;
tryparseboolean("true", parsedBoolean, booleanParsed);
if booleanParsed;
show "Parsed boolean: ";
showln parsedBoolean;
else;
showln "Boolean parsing failed";
end;
define schar parsedChar is new;
define boolean charParsed is false;
tryparseschar("Z", parsedChar, charParsed);
if charParsed;
show "Parsed character: ";
showln parsedChar;
else;
showln "Character parsing failed";
end;
showln;
showln "=== History with FOREVERY ===";
forevery(define num value within history);
if value lessthan 50;
continue;
end;
show "Large value: ";
showln value;
end;
showln;
showln "=== FOR with BREAK ===";
for(define num i is 0, i lessthan 10, redefine i is i + 1);
if i sameas 5;
break;
end;
showln i;
end;
showln;
show "Press any key to finish: ";
define schar exitKey is readk;
showln;
show "You pressed: ";
showln exitKey;
showln "Done.";