From 8f53a0c5071860fc656bb3445e82443de216636a Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Fri, 26 Jan 2024 14:45:43 -0800 Subject: add image and start of metaprogramming article --- blog/img/stack.png | Bin 0 -> 8826 bytes blog/stem.org | 28 ++++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 blog/img/stack.png (limited to 'blog') diff --git a/blog/img/stack.png b/blog/img/stack.png new file mode 100644 index 0000000..f4c2c4d Binary files /dev/null and b/blog/img/stack.png differ diff --git a/blog/stem.org b/blog/stem.org index 17820cd..25b8979 100644 --- a/blog/stem.org +++ b/blog/stem.org @@ -105,8 +105,13 @@ There are also some basic math operations you can do: : 0.750000 One can independently verify that these results are accurate. These basic math operations take /two/ things off of the stack, does the operation -on those two numbers, and then puts them back on the stack. Then, the period character prints the value and pops them off the stack. There are predefined -words for other mathematical operations too, all listed here: +on those two numbers, and then puts the new value back on the stack, deleting the old values. Then, the period character prints the value and pops +them off the stack. + +#+CAPTION: Demonstration of the stack effect of the plus word +[[file:./img/stack.png]] + +There are predefined words for other mathematical operations too, all listed here: #+begin_src stem :exports both 0.0 sin . @@ -293,3 +298,22 @@ loop-some [ dup 0 <= [ ] [ dup . 1 - loop-some ] if ] def and we can see that it actually loops. You can modify the code to do more complex looping, and in the standard library (the ~stemlib~ folder), there is a ~loop~ function that loops any code any amount of times, written by Matthew Hinton. + +* Metaprogramming +So what is this talk of metaprogramming? To put it simply, metaprogramming is a method by which one can autonomously build code and then evaluate it, +thus allowing oneself to talk about code, or make decisions to make different code based on some inputs, before running the code. So how might +we use metaprogramming? In the standard library, we define a couple of words ~dupd~, ~dupt~: +#+begin_src stem :exports both +dupd [ [ dup ] dip ] def +dupt [ [ [ dup ] dip ] dip ] def +3 2 dupd ? +#+end_src + +#+RESULTS: +: 3 +: 3 +: 2 + +which duplicates the second and third value on the stack respectively. However, we might want to define ~dupn~ for any n, which takes in an integer +and computes ~dup~ ~n~ values down. We can do that with metaprogramming, or less abstractly, we can do it by repeatedly putting quotes inside quotes, +and then we can ~eval~ the resultant quote. -- cgit