Stem
To observe alongside, I recommend after following the directions on the github page you go into the stem mission folder, discover the stemlib
folder, go into it
with cd stemlib
, after which run stem repl.stem
. Right here you’ll encounter what is named the REPL, or the learn, eval, print loop. What it’s referred to as
doesn’t matter. Simply know that it runs stem code interactively.
A fundamental phrase that prints out the highest factor on the stack and removes it’s merely a interval:
# this can be a remark, used to clarify code however does not have an effect on the result in any respect. # feedback begin with a '#'. "hi there worldn" .
hi there world
the place the n
simply signifies a newline character, mainly simply telling it to not print the “hi there world” on the identical line as the following factor printed.
You possibly can print all the stack like so:
1 2 3 [ "some quote" ] "string!" ?
1 2 3 Q: [ some quote] string!
Which prints all the stack, the place the bottom-most factor is the highest factor on the stack.
There are additionally some fundamental math operations you are able to do:
3 4 + . 3 4 - . 3 4 * . 3.0 4 / .
7 -1 12 0.750000
One can independently confirm that these outcomes are correct. These fundamental math operations take two issues off of the stack, does the operation
on these two numbers, after which places the brand new worth again on the stack, deleting the outdated values. Then, the interval character prints the worth and pops
them off the stack.
Determine 2: Demonstration of the stack impact of the plus phrase
There are predefined phrases for different mathematical operations too, all listed right here:
0.0 sin . 0.0 cos . 1.0 exp . 2.5 flooring . 2.5 ceil . 2.71828 ln .
0.000000 1.000000 2.718282 2.000000 3.000000 0.999999
These operations I’ll assume you’re accustomed to, and one can independently confirm their (approximate) validity. There are additionally comparability
and logical operations:
"hello" "hello" = . 4 3 = . 3 4 < . 3 4 > . 3 4 <= . 3 4 >= . 1 1 and . 1 0 and . 0 1 or . 0 0 or .
1 0 1 0 1 0 1 0 1 0
Which examine the primary quantity to the second quantity with a sure operation like “larger than or equals to”. The result’s a zero or one, indicating
that the assertion is both true or false, with 1 being true. With these statements, you may make selections:
3 4 < [ "3 < 4" . ] [ "3 >= 4" . ] if
3 < 4
the place the phrase if
simply checks if the third factor from the highest of the stack (the very first thing you write) is a zero or a one, and whether it is, then execute
no matter’s inside the primary quote, in any other case execute the second quote. Be aware that this wording is a little bit bit complicated as a result of the very first thing you write
can be the very last thing on the stack as a result of including new issues to the stack places the very first thing beneath the second.
Now, additionally observe that contained in the quotes we’re storing legitimate code. This can turn out to be necessary afterward as we introduce the idea of metaprogramming. First,
although, we now have to introduce a pair extra necessary predefined phrases.
[ "hello world!n" . ] eval 3 quote . [ 1 2 ] [ 3 4 ] compose . 1 [ 2 3 ] curry .
hi there world! Q: [ 3 ] Q: [ 1 2 3 4 ] Q: [ 1 2 3 ]
eval
evaluates the highest of the stack as if it have been a chunk of code; quote
places the highest of the stack in a quote after which pushes it again to
the highest of the stack; compose
combines two quotes into one; and curry
places a worth within the entrance of the quote. Be aware that a few of these operations
work for strings as effectively:
"hi there " "worldn" compose .
hi there world
And another phrases that we use to function on quotes and strings are right here:
[ 1 2 3 4 ] 1 lower . . 0 [ 5 6 7 8 ] vat . "hellonworldn" 6 lower . . 1 "asdfghjkl;" vat .
Q: [ 3 4 ] Q: [ 1 2 ] 5 world hi there s
lower
cuts a string or quote into two, the place the quantity in entrance tells lower
the place to chop. Be aware that usually in programming numbering begins
at 0, so 1 is definitely the second aspect of the quote. vat
will get the nth aspect, the place n is the first worth handed into vat
. It additionally returns the quote or string
on the stack again after, with the worth at that index on prime. There are two extra phrases that we now have to outline:
1 2 swap . . 1 2 . . "hellon" dup . . 1 2 5 [ + ] dip . .
1 2 2 1 hi there hi there 5 3
swap
simply swaps the highest two numbers on the stack, dup
simply duplicates the highest of the stack,
and dip
is simply eval
besides it does the operation one layer beneath. On this instance, it provides 1 and a couple of
as a substitute of two and 5, thus you see a 5 and a 3 printed as a substitute. Be aware that there are extra phrases, however we gained’t want them for now. Now, we’re prepared to analyze
the way to outline phrases when it comes to different phrases, or so-called compound phrases.