new example
FossilOrigin-Name: ea6ee29d8568694118c6ba0741d685af3075c7c17f29aa46a3262e8bb5880135
This commit is contained in:
parent
c41dab370e
commit
de2b823023
2 changed files with 63 additions and 1 deletions
62
example/variables-and-formulas.retro
Normal file
62
example/variables-and-formulas.retro
Normal file
|
@ -0,0 +1,62 @@
|
|||
From my mastodon feed:
|
||||
|
||||
@millihertz@oldbytes.space:
|
||||
|
||||
so i just saw this example given as a reason why
|
||||
concatenative languages tend to be called "write-only":
|
||||
|
||||
f x y z = y² + xÂ- |y|
|
||||
|
||||
: f
|
||||
drop dup dup *
|
||||
swap abs
|
||||
rot3 dup *
|
||||
swap - + ;
|
||||
|
||||
... snipped ...
|
||||
|
||||
He ends up writing the following:
|
||||
|
||||
lastly, nobody would ever write Forth like that anyway!
|
||||
|
||||
: ²+ dup * swap dup * + ;
|
||||
: y²+x²-|y| ( y x -- result )
|
||||
tuck ²+ swap abs - ;
|
||||
|
||||
Given a dislike of stack shufflers, a Retro implementation could be:
|
||||
|
||||
:f [ [ n:square ] bi@ + ] sip n:abs - ;
|
||||
|
||||
Assuming one knows the combinators, this is pretty straightforward,
|
||||
but doesn't really resemble the original formula. I decided to write
|
||||
a quick set of words to let me convert the original formula into
|
||||
something more recognizable while still being RPN.
|
||||
|
||||
:f /xyz
|
||||
x: n:square y: n:square + y: n:abs - ;
|
||||
|
||||
This adds a / sigil that maps stack values to an internal set of
|
||||
variables, whose values are returned by words with the variable names
|
||||
followed by a colon.
|
||||
|
||||
The code follows.
|
||||
|
||||
~~~
|
||||
'Vars d:create #256 allot
|
||||
|
||||
'abcdefghijklmnopqrstuvwxyz s:keep
|
||||
[ dup ':%c:_&Vars_$%c_+_fetch_; s:format s:evaluate ] s:for-each
|
||||
|
||||
:sigil:/
|
||||
s:reverse [ 'liliadst i , &Vars , ] s:for-each ; immediate
|
||||
~~~
|
||||
|
||||
And the test case:
|
||||
|
||||
```
|
||||
:f /xyz
|
||||
x: n:square y: n:square + y: n:abs - ;
|
||||
|
||||
#33 #12 #301 f n:put
|
||||
```
|
||||
|
|
@ -23,7 +23,7 @@ expectations.
|
|||
First up, constants for the file modes.
|
||||
|
||||
| # | Used For |
|
||||
| -- | ---------------------------- |
|
||||
| -- | ---------------------------- |
|
||||
| R | Mode for READING |
|
||||
| W | Mode for WRITING |
|
||||
| A | Mode for APPENDING |
|
||||
|
|
Loading…
Reference in a new issue