merge in some minor updates to the examples
FossilOrigin-Name: bde547c22b155870b9a00e14bc73feb6c165df9c78ab1b51e31f42a8bd1304d9
This commit is contained in:
parent
e44fc4be98
commit
a4e28b79d2
3 changed files with 32 additions and 19 deletions
|
@ -1,4 +1,4 @@
|
|||
# example|RosettaCode|1D-Cellular-Automota
|
||||
# 1D Cellular Automota
|
||||
|
||||
Assume an array of cells with an initial distribution of live and dead cells, and imaginary cells off the end of the array having fixed values.
|
||||
|
||||
|
@ -15,12 +15,6 @@ If, in the following table, a live cell is represented by 1 and a dead cell by 0
|
|||
110 -> 1 # Needs one neighbour to survive
|
||||
111 -> 0 # Starved to death.
|
||||
|
||||
Declare module constant (prevents reloading when using `import`):
|
||||
|
||||
~~~
|
||||
:example|RosettaCode|1D-Cellular-Automota ;
|
||||
~~~
|
||||
|
||||
I had originally written an implementation of this in RETRO 11. For RETRO 12 I took advantage of new language features and some further considerations into the rules for this task.
|
||||
|
||||
The first word, `string,` inlines a string to `here`. I'll use this to setup the initial input.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# example|99Bottles
|
||||
# 99 Bottles
|
||||
|
||||
Display the text for the *99 Bottles of Beer* song.
|
||||
|
||||
|
|
|
@ -1,23 +1,42 @@
|
|||
Traditional Forth has a CREATE/DOES> construct. RETRO allows for
|
||||
something similar using the `does` combinator.
|
||||
# Accumulator
|
||||
|
||||
An example in traditional Forth:
|
||||
## Description
|
||||
|
||||
This implements a function that takes an initial value and constructs a new function that returns the value before incrementing the stored value by 1.
|
||||
|
||||
So, given an initial value of 1, the first time the function is called, 1 is returned. The second, 2, and so on.
|
||||
|
||||
In traditional Forth, this would be done using a CREATE/DOES> construct. RETRO allows for something similar using the `does` combinator.
|
||||
|
||||
An example in a traditional Forth:
|
||||
|
||||
: acc ( n "name" -- )
|
||||
create , does> dup >r @ dup 1+ r> ! ;
|
||||
|
||||
And in RETRO, using `does` and the `bi` combinator:
|
||||
In RETRO, we could begin by rewriting this using the RETRO words:
|
||||
|
||||
:acc (ns-)
|
||||
d:create , [ dup push fetch n:inc pop store ] does ;
|
||||
|
||||
The `dup push ... pop` pattern is the `sip` combinator, so we can simplify it:
|
||||
|
||||
:acc (ns-)
|
||||
d:create , [ [ fetch n:inc ] sip store ] does ;
|
||||
|
||||
This is better, but not quite done. RETRO has a `v:inc` for incrementing variables, which would eliminate the n:inc and store. And a `bi` combinator to run two quotes against a value. So we could simplify yet again, resulting in:
|
||||
|
||||
~~~
|
||||
:acc (ns-)
|
||||
d:create , [ [ fetch ] [ v:inc ] bi ] does ;
|
||||
~~~
|
||||
|
||||
Here's a little test case:
|
||||
This removes the primitive stack shuffling, and leaves something that expresses the intent more clearly.
|
||||
|
||||
~~~
|
||||
#10 'foo acc
|
||||
foo
|
||||
foo
|
||||
foo
|
||||
~~~
|
||||
Finally, here's a little test case:
|
||||
|
||||
```
|
||||
#10 'foo acc
|
||||
foo
|
||||
foo
|
||||
foo
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue