mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-14 16:35:03 +01:00
add some things to std
This commit is contained in:
parent
c4cfa19fa9
commit
b779a51ef5
2 changed files with 52 additions and 2 deletions
|
|
@ -16,8 +16,8 @@ const INPUT = `
|
|||
extern mod std;
|
||||
|
||||
function main() = (
|
||||
std.pow(10, 2);
|
||||
);
|
||||
std.printlnInt(10000);
|
||||
);
|
||||
`;
|
||||
|
||||
function main() {
|
||||
|
|
|
|||
50
std.nil
50
std.nil
|
|
@ -1,3 +1,48 @@
|
|||
function printlnInt(x: Int) = (
|
||||
printInt(x);
|
||||
print("\n");
|
||||
);
|
||||
|
||||
function printInt(x: Int) = (
|
||||
let mag = log10(x);
|
||||
|
||||
loop (
|
||||
if mag == 0 then break;
|
||||
let base = pow(10, mag);
|
||||
|
||||
let digit = x / base;
|
||||
print(stringForDigit(digit));
|
||||
|
||||
x = x % base;
|
||||
mag = mag - 1;
|
||||
);
|
||||
|
||||
print(stringForDigit(x % 10));
|
||||
);
|
||||
|
||||
function stringForDigit(x: Int): String =
|
||||
if x == 0 then "0"
|
||||
else if x == 1 then "1"
|
||||
else if x == 2 then "2"
|
||||
else if x == 3 then "3"
|
||||
else if x == 4 then "4"
|
||||
else if x == 5 then "5"
|
||||
else if x == 6 then "6"
|
||||
else if x == 7 then "7"
|
||||
else if x == 8 then "8"
|
||||
else if x == 9 then "9"
|
||||
else trap();
|
||||
|
||||
function log10(x: Int): Int = (
|
||||
let i = 0;
|
||||
loop (
|
||||
if x < 10 then break;
|
||||
i = i + 1;
|
||||
x = x / 10;
|
||||
);
|
||||
i
|
||||
);
|
||||
|
||||
function pow(base: Int, exp: Int): Int = (
|
||||
let acc = 1;
|
||||
loop (
|
||||
|
|
@ -7,3 +52,8 @@ function pow(base: Int, exp: Int): Int = (
|
|||
);
|
||||
acc
|
||||
);
|
||||
|
||||
function println(s: String) = (
|
||||
print(s);
|
||||
print("\n");
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue