mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-26 07:04:27 +00:00
add functions: loop, coinflip, rand
This commit is contained in:
parent
80a72b616b
commit
a38ef23e17
@ -141,6 +141,7 @@ quote | `(quote (1 2 3))` | `(1 2 3)` | Return the argument without evaluation.
|
||||
Name | Example | Example result | Description
|
||||
----- | ------- | -------------- | -----------
|
||||
doc | `(doc +)` | `"Return the sum of all arguments."` | Show description of a built-in function.
|
||||
loop | `(loop (fn (a) (do (print a) (coinflip))) "hello ")` | `hello hello hello false` | Call the given function repeatedly in a loop until it returns false.
|
||||
read | `(read "(+ 1 2 3)")` | `(+ 1 2 3)` | Read a string as code and return the expression.
|
||||
print | `(print "hello world")` | `"hello world"null` | Print expression on the screen. Print returns null (which is shown due to the extra print in repl). Give optional second argument as `true` to show strings in readable format.
|
||||
error | `(error "invalid value")` | `error: invalid value` | Throw an exception with message as argument.
|
||||
@ -266,6 +267,8 @@ floor | `(floor 2.5)` | `2` | Get the next lowest integer.
|
||||
ceil | `(ceil 2.5)` | `3` | Get the next highest integer.
|
||||
pow | `(pow 2 4)` | `16` | Raise the first argument to the power of the second argument.
|
||||
sqrt | `(sqrt 2)` | `1.41` | Calculate the square root.
|
||||
coinflip | `(coinflip)` | `true` | Return true or false with equal probability.
|
||||
rand | `(rand 5 10)` | `8` | Return a random integer between given min and max values.
|
||||
|
||||
### String functions
|
||||
|
||||
|
@ -99,5 +99,15 @@ class Math implements ILib
|
||||
$env->set('sqrt', new CoreFunc('sqrt', 'Return the square root of the arguemnt.', 1, 1,
|
||||
fn ($a) => sqrt($a)
|
||||
));
|
||||
|
||||
// Random number generator
|
||||
|
||||
$env->set('coinflip', new CoreFunc('coinflip', 'Return true or false with equal probability.', 0, 0,
|
||||
fn () => boolval(mt_rand(0, 1))
|
||||
));
|
||||
|
||||
$env->set('rand', new CoreFunc('rand', 'Return a random integer between given min and max values.', 2, 2,
|
||||
fn ($min, $max) => mt_rand($min, $max)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,15 @@ class LispFactory
|
||||
}
|
||||
));
|
||||
|
||||
$env->set('loop', new CoreFunc('loop', 'Call the given function repeatedly in a loop until it returns false.', 1, -1,
|
||||
function (Func $f, ...$args) {
|
||||
do {
|
||||
$result = $f->call($args);
|
||||
} while ($result);
|
||||
return $result;
|
||||
}
|
||||
));
|
||||
|
||||
$env->set('read', new CoreFunc('read', 'Read string as code.', 1, 1,
|
||||
fn (string $a) => $reader->read($tokenizer->tokenize($a))
|
||||
));
|
||||
|
Loading…
Reference in New Issue
Block a user