Trabb Pardo–Knuth algorithm: Difference between revisions

RPL: more comment
(add RPL - idiomatic)
(RPL: more comment)
Line 3,757:
 
=={{header|RPL}}==
Idiomatic RPL is based on use of stack whenever possible, short code and minimalist UX.
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
Line 3,765 ⟶ 3,766:
≪ DUP ABS √ SWAP 3 ^ + ≫ ''''FUNC'''' STO
≪ "EnterPush number11 numbers in stack, then CONT" { } → s
HALT 11 →LIST → s
≪ 1 11 '''START''' HALT s SWAP + 's' STO '''NEXT'''
DROP 11 1
'''FOR''' j s j GET
'''FUNC'''
IF DUP 400 > THEN
DROP "Too large!"
ROT '''ELSEIFTE'''
-1 '''STEP'''
"F(" s j GET →STR + ")=" +
≫ ≫ ''''TPK'''' STO
SWAP →STR +
'''END'''
-1 '''STEP'''
≫ ≫ ''''TPK'''' STO
|
'''FUNC''' ''( x -- sqrt(abs(x))+x^3 )''
Line 3,787 ⟶ 3,785:
call a function to do an operation
if result overflows
alert user
else print result
print result
|}
<code>CONT</code> is not an instruction but a command, triggered by a specific keystroke.
On RPL versions used by HP-48 calculators and beyond, the first 32 lines of TPK can be replaced by
≪ { }
1 11 '''START''' "Enter a number" { "?" V } INPUT + '''NEXT''' → s
to slightly improve the collection of the 11 numbers, avoiding the need for the <code>CONT</code> command.
≪ DROP 1 11
{{in}}
<pre>
Line 3,806 ⟶ 3,802:
<pre>
11: "Too large!"
10: "F(3)=28.7320508076"
9: "F(0)=0"
8: "F(1)=2"
7: "F(0)=0"
6: "F(1)=2"
5: "F(0)=0"
4: "F(1)=2"
3: "F(0)=0"
2: "F(1)=2"
1: "Too large!"
</pre>
 
===Idiomatic===
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ "Push 11 numbers in stack, then CONT"
HALT 11 →LIST → s
≪ DROP 11 1
'''FOR''' j s j GET
'''FUNC'''
DUP 400 >
"Too large!"
ROT '''IFTE'''
-1 '''STEP'''
≫ ≫ ''''TPK'''' STO
|
'''TPK''' ''( -- report )''
ask for 11 numbers to be read into a sequence S
reverse sequence S
for each item in sequence S
call a function to do an operation
if result overflows
alert user
else print result
|}
 
=={{header|Ruby}}==
1,150

edits