Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

lisp can sum a list of numbers with 2 parens and a +, can forth use loop to make something similar?

415 views
Skip to first unread message

the_gavino_himself

unread,
Jan 22, 2015, 10:34:36 PM1/22/15
to
can forth do something similar with loop and the return stack?

(+ 3 4 5 6 7 8)

seems nicer than

3 4 5 6 7 8 + + + + + .

it seems loop and the stack could be made to do something like lisp does

foxaudio...@gmail.com

unread,
Jan 22, 2015, 11:08:36 PM1/22/15
to
Forth is a meta language. This not really equivalent to LISP, but is how one might do it with Forth if something similar looking was prerequisite.

: ( ; \ sugar for the example, could be used to store the stack position.

: +) depth 1 ?do + loop ;


( 3 4 5 6 7 8 +) .s
33 <-Top ok


Time to try one yourself

BF



Mark Wills

unread,
Jan 23, 2015, 5:00:05 AM1/23/15
to
Nice example, BF. Hat's off!

Coos Haak

unread,
Jan 23, 2015, 8:24:02 AM1/23/15
to
Op Thu, 22 Jan 2015 20:08:35 -0800 (PST) schreef
foxaudio...@gmail.com:
This allows nesting in Gforth:
: (
depth 1+ r> 2>r
;

: cond
depth j >
;

: done
2r> rdrop 2>r
;

: +)
begin cond
while +
repeat
done
;

: *)
begin cond
while *
repeat
done
;

( 1 2 3 4 5 6 7 8 9 10 +) .
( 1 2 3 4 5 6 7 8 9 10 *) .
( ( 6 ( 3 3 +) *) ( ( 4 4 +) 8 *) +) .
--
Coos

CHForth, 16 bit DOS applications
http://home.hccnet.nl/j.j.haak/forth.html

the_gavino_himself

unread,
Jan 23, 2015, 4:10:00 PM1/23/15
to
So one I know more forth this will seem trivial?

Its the variable number of inputs that kinda caught me.


Could this be even simpler like bunch of numbers, some word, ---> produces total?

WJ

unread,
Jan 23, 2015, 8:44:11 PM1/23/15
to
But in Scheme and Lisp, + can be used to add up a list
of numbers that already exists:

Gauche Scheme:

(use srfi-27)
(define random-numbers (map (^_ (random-integer 1000)) (iota 999000)))
(apply + random-numbers)
===>
499025204

(length random-numbers)
===>
999000


Another way:

(fold + 0 random-numbers)
===>
499025204

empty-buffers

unread,
Jan 28, 2015, 7:38:22 PM1/28/15
to
In comp.lang.forth, foxaudio...@gmail.com wrote:

> Forth is a meta language. This not really equivalent to LISP, but is how
> one might do it with Forth if something similar looking was prerequisite.
>
>: ( ; \ sugar for the example, could be used to store the stack position.
>
>: +) depth 1 ?do + loop ;
>
>
> ( 3 4 5 6 7 8 +) .s
> 33 <-Top ok

But I've got a feeling, I see possible problem here: the numbers from the
row have to be put onto stack first. What if the stack is "shallow" - and the
row is long? Using C, and , is a bit cumbersome - but on the other hand: it
frees the stack immediately on the rule: "number-gone-number-gone-...".

I wonder is it possible to implement something like the above "stack-safe"
way? Yes, actually normally there's no fear - but I read somewhere, that for
example Chuck Moore quite often used in his work stacks say 16 cells deep
(or even smaller).
--
"Yes, this is the bad end you've come to, and two and two is - as it always
was..." - began Trurl, but just then the machine made a faint, barely audible
croaking noise and said, for the last time: "SEVEN".

Coos Haak

unread,
Jan 28, 2015, 8:58:14 PM1/28/15
to
Op Thu, 29 Jan 2015 00:38:21 +0000 (UTC) schreef empty-buffers:

> In comp.lang.forth, foxaudio...@gmail.com wrote:
>
>> Forth is a meta language. This not really equivalent to LISP, but is how
>> one might do it with Forth if something similar looking was prerequisite.
>>
>>: ( ; \ sugar for the example, could be used to store the stack position.
>>
>>: +) depth 1 ?do + loop ;
>>
>>
>> ( 3 4 5 6 7 8 +) .s
>> 33 <-Top ok
>
> But I've got a feeling, I see possible problem here: the numbers from the
> row have to be put onto stack first. What if the stack is "shallow" - and the
> row is long? Using C, and , is a bit cumbersome - but on the other hand: it
> frees the stack immediately on the rule: "number-gone-number-gone-...".
>
> I wonder is it possible to implement something like the above "stack-safe"
> way? Yes, actually normally there's no fear - but I read somewhere, that for
> example Chuck Moore quite often used in his work stacks say 16 cells deep
> (or even smaller).

I would not worry, big systems like Gforth have around 16 K stacks so there
is no problem. Even my 16 bit DOS based Forth can have more than 1 K cells
deep stacks. Catch/throw and nested including use lots of stack space.

It could be a problem with embedded CPUs if one would stash bytes this way
as an alternative for assembly code. In that case, use a decent cross Forth
compiler that should have a built-in (Forth) assembler.

Elizabeth D. Rather

unread,
Jan 28, 2015, 9:34:42 PM1/28/15
to
On 1/28/15 2:38 PM, empty-buffers wrote:
> In comp.lang.forth, foxaudio...@gmail.com wrote:
>
>> Forth is a meta language. This not really equivalent to LISP, but is how
>> one might do it with Forth if something similar looking was prerequisite.
>>
>> : ( ; \ sugar for the example, could be used to store the stack position.
>>
>> : +) depth 1 ?do + loop ;
>>
>>
>> ( 3 4 5 6 7 8 +) .s
>> 33 <-Top ok
>
> But I've got a feeling, I see possible problem here: the numbers from the
> row have to be put onto stack first. What if the stack is "shallow" - and the
> row is long? Using C, and , is a bit cumbersome - but on the other hand: it
> frees the stack immediately on the rule: "number-gone-number-gone-...".
>
> I wonder is it possible to implement something like the above "stack-safe"
> way? Yes, actually normally there's no fear - but I read somewhere, that for
> example Chuck Moore quite often used in his work stacks say 16 cells deep
> (or even smaller).
>

This is an example of the need to consider possible use in your design.
You should know, when writing such a word, what kind of platforms it's
going to be used on and what size (in general) strings you're going to
use it on.

Cheers,
Elizabeth

--
==================================================
Elizabeth D. Rather (US & Canada) 800-55-FORTH
FORTH Inc. +1 310.999.6784
5959 West Century Blvd. Suite 700
Los Angeles, CA 90045
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================

beeflo

unread,
Jan 29, 2015, 11:22:37 AM1/29/15
to
On Friday, January 23, 2015 at 6:24:02 AM UTC-7, Coos Haak wrote:
> Op Thu, 22 Jan 2015 20:08:35 -0800 (PST) schreef
>
I hope you don't mind, but I asked a question about this code on stackoverflow.com.

The link is: http://stackoverflow.com/questions/28219321/how-do-2r-and-2r-work

I asked it there because I think the question is a good fit for stack overflow.

Elizabeth D. Rather

unread,
Jan 29, 2015, 4:35:36 PM1/29/15
to
On 1/29/15 6:22 AM, beeflo wrote:
...
>
> I hope you don't mind, but I asked a question about this code on stackoverflow.com.
>
> The link is: http://stackoverflow.com/questions/28219321/how-do-2r-and-2r-work
>
> I asked it there because I think the question is a good fit for stack overflow.

You asked what 2>R and 2R> do. I see there's an answer, but to handle it
briefly here, 2>R pops 2 items off the data stack and pushes them onto
the Return Stack, and 2R> pops them off the Return Stack and pushes them
on to the data stack.

The post quote gforth docs as saying:

2>r d – R:d core-ext “two-to-r”
2r> R:d – d core-ext “two-r-from”

I would criticize this stack description because when you use 'd' you
imply that the two cells are part of a double integer, whereas they are
really just two possibly unrelated cells. I think that description is
part of what's confusing beeflo, and I would encourage the gforth
maintainers to fix the description.

the_gavino_himself

unread,
Jan 29, 2015, 7:04:38 PM1/29/15
to
On Thursday, January 22, 2015 at 10:34:36 PM UTC-5, the_gavino_himself wrote:
So in the simplest sense using gforth:

: +m depth 1 ?do + loop . ;
ok
4 55 67 +m 126 ok


Wow forth kicks ass.

How does forth handle complexity once things grow?
I remember reading Charles Moore saying complexity can be moved into data.
Lispers say most complex c programs require a half assed lisp to be implemented inside the program.
Also, I read Jeff Fox (rip) saying that networked forth with forth-linda allowed easy multi node clusters to attack problems.
Does forth get used that way still?
His essay said something like a remote execution was as easy as loop or something.
Ultratechnology is an inspiring site.

Paul E Bennett

unread,
Jan 30, 2015, 3:46:26 AM1/30/15
to
the_gavino_himself wrote:

> Wow forth kicks ass.
>
> How does forth handle complexity once things grow?
> I remember reading Charles Moore saying complexity can be moved into data.
> Lispers say most complex c programs require a half assed lisp to be
> implemented inside the program. Also, I read Jeff Fox (rip) saying that
> networked forth with forth-linda allowed easy multi node clusters to
> attack problems. Does forth get used that way still? His essay said
> something like a remote execution was as easy as loop or something.
> Ultratechnology is an inspiring site.

Part of the way Fprth handles the complexity is that during design you are
able to identify the components you need to resolve a particular solution.
You can design what looks like something more complex but it remains an
entity simple enough to describe, consider and test.

For example, take the humble carburettor. It has a body, some small shafts,
a needle, a butterfly valve, some nuts springs and screws. Each of the
individual items are part of the make up, some components even consist of
other components fixed together. Yet, you can refer to the whole as a
carburettor and, because people know what that is, you can speak of it in
its part in the workings of an engine.

This is taking the Component Oriented view of a system. The nice thing about
components is that they are self-complete, fully describable items with
definite interfaces and a thoroughly testable as an entity. Forth is a very
nice Componented Oriented Development Environment (see my EuroForth 2014
paper on the topic for more material).

--
********************************************************************
Paul E. Bennett IEng MIET.....<email://Paul_E....@topmail.co.uk>
Forth based HIDECS Consultancy.............<http://www.hidecs.co.uk>
Mob: +44 (0)7811-639972
Tel: +44 (0)1235-510979
Going Forth Safely ..... EBA. www.electric-boat-association.org.uk..
********************************************************************

skydda...@gmail.com

unread,
Jan 30, 2015, 5:39:46 AM1/30/15
to
VARIABLE SSP
: { DEPTH SSP ! ;
: } DEPTH SSP @ - ' SSP ! 1 DO SSP @ EXECUTE LOOP ;

{ 1 2 3 4 5 } * gives 120
{ 1 2 3 4 5 } + gives 15

Alex McDonald

unread,
Jan 30, 2015, 6:32:18 AM1/30/15
to
on 30/01/2015 10:39:47, skydda wrote:

>
> VARIABLE SSP
>: { DEPTH SSP ! ;
>: } DEPTH SSP @ - ' SSP ! 1 DO SSP @ EXECUTE LOOP ;
>
> { 1 2 3 4 5 } * gives 120
> { 1 2 3 4 5 } + gives 15
>

Except it won't compile code that works when executed.

: X { 2 3 4 } * ;

throws an error. At run time, the ' tick is trying to parse for a token
to derive its xt. Making } IMMEDIATE doesn't help, since the DO EXECUTE
LOOP is taking place at the wrong time.

lehs

unread,
Jan 30, 2015, 7:08:34 AM1/30/15
to
I couldn't imagine that anyone would need a routine like X, but of cause it is possible to do even such an extension in Forth. It's just a little more complicated.

Anton Ertl

unread,
Jan 30, 2015, 11:26:01 AM1/30/15
to
"Elizabeth D. Rather" <era...@forth.com> writes:
>The post quote gforth docs as saying:
>
>2>r d – R:d core-ext “two-to-r”
>2r> R:d – d core-ext “two-r-from”
>
>I would criticize this stack description because when you use 'd' you
>imply that the two cells are part of a double integer, whereas they are
>really just two possibly unrelated cells. I think that description is
>part of what's confusing beeflo, and I would encourage the gforth
>maintainers to fix the description.

Interestingly, I had earlier defined

2>r ( w1 w2 -- R:w1 R:w2 ) core-ext two_to_r
2r> ( R:w1 R:w2 -- w1 w2 ) core-ext two_r_from

and Bernd changed this in 2003 to

2>r ( d -- R:d ) core-ext two_to_r
2r> ( R:d -- d ) core-ext two_r_from

with the puzzling comment

|Fixed reverse ordering

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
EuroForth 2014: http://www.euroforth.org/ef14/

Anton Ertl

unread,
Jan 30, 2015, 11:31:16 AM1/30/15
to
Next: make it work for the nested case:

{ 9 8 { 7 6 5 } + 4 } *

should give 5184. Making it work in compile state is an advanced
topic (as Alex McDonald requested), because it opens a can of worms.

lehs

unread,
Jan 30, 2015, 11:38:54 AM1/30/15
to
Den fredag 30 januari 2015 kl. 12:32:18 UTC+1 skrev Alex McDonald:
SP-FORTH - ANS FORTH 94 for Win95/98/Me/NT/2k/XP/Vista
Open source project at http://spf.sf.net
Russian FIG at http://www.forth.org.ru ; Started by A.Cherezov
Version 4.20 Build 001 at 21.Jan.2009

VARIABLE SSP
Ok
: { DEPTH SSP ! ;
Ok

Ok
: EXC SSP ! 1 DO SSP @ EXECUTE LOOP ;
Ok

Ok
: RBR DEPTH SSP @ - ;
Ok

Ok
: } STATE @
IF POSTPONE RBR
[COMPILE] '
POSTPONE LITERAL
POSTPONE EXC
ELSE RBR ' EXC
THEN
; IMMEDIATE
Ok

Ok
{ 2 3 5 7 } *
Ok ( 210 )

Ok ( 210 )
: TEST { 2 3 5 7 } + ;
Ok ( 210 )

Ok ( 210 )
TEST
Ok ( 210 17 )

But, it's difficult to compare LISP and FORTH. LISP has a great generality of one kind while FORTH has a great generality of an other kind.

Bernd Paysan

unread,
Jan 30, 2015, 3:48:15 PM1/30/15
to
Anton Ertl wrote:

> "Elizabeth D. Rather" <era...@forth.com> writes:
>>The post quote gforth docs as saying:
>>
>>2>r d � R:d core-ext �two-to-r�
>>2r> R:d � d core-ext �two-r-from�
>>
>>I would criticize this stack description because when you use 'd' you
>>imply that the two cells are part of a double integer, whereas they are
>>really just two possibly unrelated cells. I think that description is
>>part of what's confusing beeflo, and I would encourage the gforth
>>maintainers to fix the description.
>
> Interestingly, I had earlier defined
>
> 2>r ( w1 w2 -- R:w1 R:w2 ) core-ext two_to_r
> 2r> ( R:w1 R:w2 -- w1 w2 ) core-ext two_r_from
>
> and Bernd changed this in 2003 to
>
> 2>r ( d -- R:d ) core-ext two_to_r
> 2r> ( R:d -- d ) core-ext two_r_from
>
> with the puzzling comment
>
> |Fixed reverse ordering

Not sure, but the context of that complete patchset was mostly C foreign
funciton interface, and maybe that reverse ordering referred to the C
foreign function interface.

--
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://bernd-paysan.de/
net2o ID: kQusJzA;7*?t=uy@X}1GWr!+0qqp_Cn176t4(dQ*

Coos Haak

unread,
Jan 30, 2015, 4:17:13 PM1/30/15
to
Op Fri, 30 Jan 2015 08:38:53 -0800 (PST) schreef lehs:

<snip>
>: } STATE @
> IF POSTPONE RBR
> [COMPILE] '
> POSTPONE LITERAL
> POSTPONE EXC
> ELSE RBR ' EXC
> THEN
> ; IMMEDIATE

If SP-FORTH is truly ANS FORTH 94 compatible, then ' (tick) is not
immediate. Rewrite this as:
: } STATE @
IF POSTPONE RBR
POSTPONE '
POSTPONE EXC
ELSE RBR ' EXC
THEN
; IMMEDIATE

lehs

unread,
Jan 31, 2015, 1:16:50 AM1/31/15
to
You're right, tick wasn't immediate! But then [COMPILE] become a no-operation.
It should be:

: } STATE @
IF POSTPONE RBR
'
POSTPONE LITERAL
POSTPONE EXC
ELSE RBR ' EXC
THEN
; IMMEDIATE

I really don't know if SP-Forth is truly ANS FORTH 94. When I retired I tried to find a FIG-FORTH but couldn't find any. I found SP and like it.

Coos Haak

unread,
Jan 31, 2015, 6:34:28 PM1/31/15
to
Op Fri, 30 Jan 2015 22:16:48 -0800 (PST) schreef lehs:
It seems that the doubt I expressed was false. Stay with SP-Forth.
FIG-FORTH is too old (around 1977).

lehs

unread,
Feb 1, 2015, 3:35:17 AM2/1/15
to
No! FIG-FORTH would be perfect for me. No prohibitions! Everything was possible and FORTH Dimension was filled with smart routines that extended the compiler. Those were the days!

lehs

unread,
Feb 1, 2015, 4:24:39 AM2/1/15
to
I didn't notice your answer, but you are right about the nestling. It needs local variables or an extra stack. Unfortunately I can't use the R-stack as above on SP-FORTH, but it is easy to define a small X-stack:

CELL NEGATE CONSTANT -CELL
Ok
CREATE XST HERE , 0x100 ALLOT ALIGN
Ok
: >X ( x -- ) CELL XST +! XST @ ! ;
Ok
: X ( -- x ) XST @ @ ;
Ok
: X> ( -- x ) X -CELL XST +! ;
Ok
: { DEPTH >X ;
Ok
: EXC >X 1 DO X EXECUTE LOOP X> DROP ;
Ok
: RBR DEPTH X> - ;
Ok
: } STATE @
IF POSTPONE RBR
'
POSTPONE LITERAL
POSTPONE EXC
ELSE RBR ' EXC
THEN
; IMMEDIATE
Ok
{ 2 3 5 7 } *
Ok ( 210 )
: TEST { 2 3 5 7 } + ; TEST
Ok ( 210 17 )
{ 9 8 { 7 6 5 } + 4 } *
Ok ( 210 17 5184 )

No! I don't think that the Forth-compiler includes a can of worms. Any other compiler might, but not a Forth-compiler. Charles Moore really invented something that time. The snaky thing about Forth is that you may be encouraged to make utterly useless routines as mine above, instead making sound Forth routines as above mine.

Coos Haak

unread,
Feb 1, 2015, 8:29:03 AM2/1/15
to
Op Sun, 1 Feb 2015 00:35:16 -0800 (PST) schreef lehs:

> No! FIG-FORTH would be perfect for me. No prohibitions! Everything was possible and FORTH Dimension was filled with smart routines that extended the compiler. Those were the days!

But you run in a lot of problems: WORD does not leave an address, you can't
use CREATE in interpretive mode, ' is immediate and like FIND leaves the
address of the parameter field not the execution token. VOCABULARYs are
immediate for no obvious reason, CELL+ and CELLS do not exist...

lehs

unread,
Feb 1, 2015, 10:42:27 AM2/1/15
to
Are you joking? How can such things be problems for a hobbyist? I'm in it for fun and FIG-Forth was so simple that I could understand and overview the whole system. Not so efficient but fun.

Anton Ertl

unread,
Feb 1, 2015, 11:19:20 AM2/1/15
to
lehs <skydda...@gmail.com> writes:
>Den fredag 30 januari 2015 kl. 17:31:16 UTC+1 skrev Anton Ertl:
>> Next: make it work for the nested case:
>>=20
>> { 9 8 { 7 6 5 } + 4 } *
>>=20
>> should give 5184.
...
>I didn't notice your answer, but you are right about the nestling. It needs=
> local variables or an extra stack.

It's possible to do it without extra stack, by storing the old value
of SSP on the data stack. Implementing this is left as an exercise
for the reader.

>: } STATE @
> IF POSTPONE RBR
> '
> POSTPONE LITERAL
> POSTPONE EXC
> ELSE RBR ' EXC
> THEN
>; IMMEDIATE
...
>No! I don't think that the Forth-compiler includes a can of worms.

Using STATE is a pretty good indication that the can is open. You may
not have noticed the worms yet, but they are there.

Albert van der Horst

unread,
Feb 1, 2015, 12:24:16 PM2/1/15
to
In article <6815a5ee-cd6d-4934...@googlegroups.com>,
Inasfar FIG-Forth has advantages, they can be retained while slightly
modifying a FIG-Forth implementation to make it more ISO compatible.
- The implementation model need not be changed substantially in
order to have e.g. 32 bit integers.
- BUILD / DOES> is not essentially more powerful than CREATE / DOES.
- ACCEPT is merely a cleaned up version of EXCEPT .
- etc

A compatibility harness can be used to run your extremely valuable
thoroughly debugged FIG-Forth programs. These kind of programs
come with a regression test, such that the compatibility harness
can be certified easily.

Groetjes Albert
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

lehs

unread,
Feb 1, 2015, 12:58:34 PM2/1/15
to
But my problem is that ANS-Forth is a stranger while FIG-Forth was not. The new systems are to complicated for me. The speed is wonderful but I valued the simplicity in the old systems.

Hälsningar Lars-Erik Svahn

lehs

unread,
Feb 1, 2015, 1:54:28 PM2/1/15
to
I'm aware of that it often is better with an other kind of solution, but I have really never seen any "worm-problems" with those constructions.

Will you please explain?

Elizabeth D. Rather

unread,
Feb 1, 2015, 1:59:12 PM2/1/15
to
On 1/31/15 10:35 PM, lehs wrote:
> Den söndag 1 februari 2015 kl. 00:34:28 UTC+1 skrev Coos Haak:
...
>>> I really don't know if SP-Forth is truly ANS FORTH 94. When I retired I tried to find a FIG-FORTH but couldn't find any. I found SP and like it.
>>
>> It seems that the doubt I expressed was false. Stay with SP-Forth.
>> FIG-FORTH is too old (around 1977).
>> --
>> Coos
>>
>> CHForth, 16 bit DOS applications
>> http://home.hccnet.nl/j.j.haak/forth.html
>
> No! FIG-FORTH would be perfect for me. No prohibitions! Everything was possible and FORTH Dimension was filled with smart routines that extended the compiler. Those were the days!
>

There are no prohibitions in ANS Forth or Forth200x. What these
standards *do* offer is *guarantees* of portability of application code
if you follow their recommendations. If you have no interest in porting
your application code to other Forth systems, you can do whatever you
want to.

The standards (and systems that follow them) also include a lot of smart
stuff and additional ways to facilitate extending the compiler. Also
modern systems tend to be faster and better documented.

lehs

unread,
Feb 1, 2015, 3:06:35 PM2/1/15
to
I don't think that the development of FORTH is bad as such, just that it doesn't suit me that good. The less advanced FIG-Forth was much easier for me to play around with.

One good thing is that I do a lot more "extrovert" programs today, that is, using Forth for other reasons than exploring Forth.

I have reasons to be very grateful to you and Charles Moore for creating Forth and spreading it out in the world. Thank you very much!

Albert van der Horst

unread,
Feb 1, 2015, 7:46:18 PM2/1/15
to
In article <e04935d7-4fcf-457e...@googlegroups.com>,
Then you may be interested in yourforth. Check it out. It is my effort
to make the simplest possible Forth and it is quite similar to FIG-Forth.

>
>Hälsningar Lars-Erik Svahn

hughag...@gmail.com

unread,
Feb 8, 2015, 9:07:27 PM2/8/15
to
On Thursday, January 22, 2015 at 8:34:36 PM UTC-7, the_gavino_himself wrote:
> can forth do something similar with loop and the return stack?
>
> (+ 3 4 5 6 7 8)
>
> seems nicer than
>
> 3 4 5 6 7 8 + + + + + .
>
> it seems loop and the stack could be made to do something like lisp does

In the novice package (http://www.forth.org/novice.html) I have RATIO.4TH (and RATIO.TXT as documentation). This allows arithmetic on lists of rational numbers (the numbers are a numerator and denominator pair). Because these are lists of values, they do pretty much what you are describing --- the syntax is not Lisp-like though, which seems to be what you want.

hughag...@gmail.com

unread,
Feb 8, 2015, 9:43:53 PM2/8/15
to
On Sunday, February 1, 2015 at 11:59:12 AM UTC-7, Elizabeth D. Rather wrote:
> There are no prohibitions in ANS Forth or Forth200x. What these
> standards *do* offer is *guarantees* of portability of application code
> if you follow their recommendations.

LOL --- you are a super-salesperson! --- nobody else in the world could deliver a preposterous statement like this with a straight face.

ANS-Forth is a complete failure at portability. Throughout the ANS-Forth document, we read that a feature may work one way or may work another way, may be available or may not be available. ANS-Forth is a standard that doesn't standardize anything --- pretty much any non-trivial program is going to be vendor-specific (and the programmer will think that it is ANS-Forth until he actually tries to compile and run it on another Forth system).

I made the novice package ANS-Forth, so it will work under any ANS-Forth system, but this was not easy. Supposedly FFL is ANS-Forth, but I haven't verified this. Anton Ertl himself says that there is no way to determine if Forth code is ANS-Forth or not, except by testing it under a variety of ANS-Forth systems --- and a lot of the differences between the systems are subtle, so a casual test may not find the dependency, but it will show up eventually. Also, I have found bugs in all of the major ANS-Forth implementations (SwiftForth, Gforth, Win32Forth and VFX), so portable ANS-Forth code needs to work around these bugs (I've seen Anton Ertl post code that will crash SwiftForth-v2; in all likelihood, he only tests his code under Gforth). Anyway, FFL lacks a lot of features that my novice package has, so I don't have any motivation to spend the considerable amount of time needed to test FFL --- in my experience, getting code to work on all major ANS-Forth systems takes up the majority of the time involved in developing the novice package.

As the chairperson of the ANS-Forth committee, you should give me a medal for being the only person on comp.lang.forth who has written code that can actually compile and run under any ANS-Forth system --- nobody else puts in the time and effort needed to do this --- also, everybody else is writing pretty trivial code (judging from their comments regarding the control-flow stack, it is obvious that Alex McDonald and Andrew Haley have never written a meta-compiling word in their entire Forth careers).

Ron Aaron

unread,
Feb 9, 2015, 1:36:33 AM2/9/15
to

On 02/09/2015 04:43, hughag...@gmail.com wrote:
> ANS-Forth is a complete failure at portability. Throughout the ANS-Forth document, we read that a feature may work one way or may work another way,...

FWIW, ANS-Forth is about as portable as C. Similar caveats apply with C
programs: a "standard library" function may or may not be available, the
CPU may be of different endianness, the int size may be 2 or 4 or 8
bytes, etc.

That's why there are preprocessor directives in C and similar
environmental queries in ANS-Forth. If you *want* your code to be
portable between vendors, you have to be aware of where the differences
are, and code for them. It does not mean that your code will run
"vanilla" without any changes.

I've had a ton of experience with C and portability; it can be a
nightmare. So in Reva Forth and now in 8th, I made no attempt
whatsoever to be ANS compliant, since that was not a priority for me.
Other people, other needs.

WJ

unread,
Feb 9, 2015, 3:56:23 AM2/9/15
to
the_gavino_himself wrote:

> can forth do something similar with loop and the return stack?
>
> (+ 3 4 5 6 7 8)
>
> seems nicer than
>
> 3 4 5 6 7 8 + + + + + .
>
> it seems loop and the stack could be made to do something like lisp does

Calculate the average of the numbers in a list, traversing
it only once and making no assignments to a variable:

Gauche Scheme:

(define (average numbers)
(apply /
(fold (lambda (x accum) (map + (list x 1) accum))
'(0 0)
numbers)))

(define numbers `(,@(iota 9 -9 2) 633 ,@(iota 9 22)))
numbers
===>
(-9 -7 -5 -3 -1 1 3 5 7 633 22 23 24 25 26 27 28 29 30)

(average numbers)
===>
858/19

(inexact (average numbers))
===>
45.1578947368421


The easy way would be

(/. (apply + numbers) (length numbers))
===>
45.1578947368421

but that traverses the list twice: once to add up the numbers
and once to determine the length of the list.

Doug Hoffman

unread,
Feb 9, 2015, 8:00:05 AM2/9/15
to
: ave{ 0e 0
begin parse-name 2dup s" }" compare
while >float drop f+ 1+
repeat 2drop s>f f/ f. ;

ave{ -9 -7 -5 -3 -1 1 3 5 7 633 22 23 24 25 26 27 28 29 30 }
45.1578947368421 ok

\ the easier way would be to use library code:
f{ -9 -7 -5 -3 -1 1 3 5 7 633 22 23 24 25 26 27 28 29 30 }
average: f. 45.1578947368421 ok


Unknown

unread,
Feb 11, 2015, 4:19:54 AM2/11/15
to
Computing is no longer 'about computors' it's about human cognition:
saving effort. Yes, minimal text is part of saving-effort.
But recognise & select [ie. menu] instead of 'coding' is cheaper.
Also color economically adds cognitive power.
Where's the VISUAL forth?

WJ

unread,
Dec 28, 2015, 6:13:03 PM12/28/15
to
the_gavino_himself wrote:

> can forth do something similar with loop and the return stack?
>
> (+ 3 4 5 6 7 8)
>
> seems nicer than
>
> 3 4 5 6 7 8 + + + + + .
>
> it seems loop and the stack could be made to do something like lisp does

Ruby:

(3..8).reduce(:+)
==>33

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunkers-5.html
https://www.youtube.com/watch?v=EEl_1HWFRfo

Ron Aaron

unread,
Dec 29, 2015, 12:20:57 AM12/29/15
to


On 29/12/2015 01:10, WJ wrote:
> the_gavino_himself wrote:
>
>> can forth do something similar with loop and the return stack?
>>
>> (+ 3 4 5 6 7 8)
>>
>> seems nicer than
>>
>> 3 4 5 6 7 8 + + + + + .
>>
>> it seems loop and the stack could be made to do something like lisp does
>
> Ruby:
>
> (3..8).reduce(:+)
> ==>33
>

8th:

: range \ low high -- a
[] ' a:push
2swap loop ;

3 8 range ' n:+ 0 a:reduce .
33
ok>
0 new messages