Daily Note - 2024-08-20
Hey, I'm Hanno! These are my daily notes on Crosscut, the programming language I'm creating. If you have any questions, comments, or feedback, please get in touch!
This note was published before Crosscut was called Crosscut! If it refers to "Caterpillar", that is the old name, just so you know.
The other day, I presented
how common conditional constructs can map to
pattern-matching functions. But there's more to
control flow! Today, let's look at iteration, starting
with a simple loop
in Rust:
loop {
do_this_forever();
}
In Caterpillar, this could look like this:
{
|| do_this_forever recur
}
eval
Here we define an anonymous function that just calls
itself forever. (recur
doesn't exist in the language right now. It would call
the current function recursively, without having to name
it. This is not possible yet for anonymous functions,
but named ones could just call themselves by name
instead.)
A slightly more complex loop:
let mut i = 0;
while i < 10 {
do_the_thing();
i += 1;
}
And the Caterpillar version:
0
{
|10|
# we're done
|n|
do_the_thing
n 1 + recur
}
eval
As with conditionals, I expect that Caterpillar will
have more convenient constructs built on top of this, as
syntax sugar. (And with Caterpillar being a functional
language, I expect those to be of the map
,
filter
, and fold
variety, rather than the more imperative loops I've
shown here. But the point was to demonstrate how simple
loops can map to pattern-matching functions.)