1 Crash Course
These warm-up exercises are meant to give you a sense of Rhombus and where we’re trying to go. These exercises are not about metaprogramming in the sense of the rest of the tutorial. We use interpreters merely as a domain that we expect to be familiar to anyone following the tutorial.
Skip the docs, because Rhombus looks like a lot of things you’ve seen before.
1.1 Exercise
Open interp1.rhm and try the exercises listed at the top of the file.
1.2 Exercise
Open interp2.rhm and try the exercises listed at the top of the file.
If you’re new to Rhombus, and because you followed the instructions to skip the docs, you’ll need to know about ellipses:
When ... is used in to match a pattern in a binding position, each variable before the ... stands for a repetition. It binds matching any number of times.
For example, it can be used for multi-argument functions:
> ignore_all_args(0)
"ok"
> ignore_all_args()
"ok"
> ignore_all_args(0, 1, 2)
"ok"
It can also be used inside lists in a single argument:Expression forms such as function calls, list constructions, and map (dictionary) constructions also recognize .... They duplicate the form before the ..., which must include a reference to a repetition. The referenced repetition determines the number times the expression is repeated.
> add_to_all(10, [1, 2, 3])
[11, 12, 13]
1.3 Exercise
Open interp3.rhm and try the exercises listed at the top of the file.
This exercise uses syntax objects. Terms surrounded by single quotes '…' are not strings, but are data structures. They are typically used to represent program expressions and avoid all the problems of using strings to represent expressions. They must conform to shrubbery notation (don’t read those docs!) in the same way that quoted Lisp and Scheme terms must conform to S-expression notation. Like S-expressions, some notational choices (such as the presence of leading zeros in numbers) do not affect the meaning of the syntax object, so '1 + 0002' (with extra spaces zeros) is the same as '1 + 2'. Unlike S-expressions, syntax objects are a sophisticated data structure that can even help track binding information.
Using '…' in a pattern-matching position accepts a matching syntax object, where $ escapes back to binding mode. Similarly, using $ escapes in a '…' expression escapes back to expression mode and substitutes the expression’s result into the syntax object.