Article View: net.lang.forth
Article #268Re: Virtues of Forth
From: Steve Solomon
Date: Mon, 06 Jan 1986 20:01
Date: Mon, 06 Jan 1986 20:01
78 lines
4096 bytes
4096 bytes
In the discussion on the virtues/flaws of Forth vs. other languages, the
friendly user who posted a reprint article (I lost his name in my kill ring--
so much for EMACS) states:
> 3) Forward referencing (including recursion) is not normal and must be
> done by manipulating compiled code. Bye-bye portability. Indeed, I have
> never seen anyone write a recursive Forth routine (although it can be
> done). Bye-bye AI.
As an an AI researcher looking into implementing semi-intelligent programs
on micros, but disappointed by how slowly LISP crawls along on a PC, I've
recently become interested in Forth as an alternative to LISP.
My credentials in Forth are not what one would call the highest, since I've
only started, but the statement about recursive routines surprised me even
before I found the following excerpt, from FORTH, W.P. Salman, O. Tisserand,
and B. Toulout, Springer-Verlag, 1984*, pg. 107, defining the quintessential
factorial routine:
The correct definition of FACT is then
: FACT
SMUDGE
IF
DUP 1 - FACT *
ELSE
1
THEN
SMUDGE
; <Return> OK
Admittedly, this is hard to read (at first glance). But it IS recursion.
Also, I was able to figure out what was going on with considerably less
trouble than what has been intimated in recent postings, just from my reading
the text. An explanation:
1) The SMUDGE works apparently like a forward declaration in Pascal. You
need to imbed your code in a recursive definition between two SMUDGE's because
the final ";" toggles the same bit in the word definition as SMUDGE, and with
only the top SMUDGE it and the semi-colon would cancel each other out.
2) The "IF" syntax in Forth works as follows (pg. 28):
condition IF process 1 ELSE process 2 THEN
The condition is the boolean value at the top of the data stack, 0 = false,
true otherwise. So "DUP 1 - FACT *" is really the THEN part of the conditional
(inductive) part. In this case, if the top of the stack (the value implicitly
passed to FACT) is not zero, then duplicate that value on the stack, subtract
1, then find the FACT of it, and multiply. ELSE when the value is 0, return 1.
The advantage of Forth over assembly language in this case is that the data
and return stacks are given; if I wanted to program recursively in assembly
language, I would have to construct a run-time program stack with the
addresses of the last invoked routines on it. At least Forth takes care of
this drudgery for me.
What makes Forth attractive for me is its compactness, its run-time
extensibility, and its multitasking capabilities, among other features.
From what little I've read, Forth also reminds of LISP. Vocabularies seem
a little like LISP packages, and there are other similarities. The comment
about Forth being like LISP without parentheses is apt, I have the feeling.
I am especially interested in using Forth to write a compiler for an unnamed
pattern matching AI language to be run on micros, and as everyone knows,
parsing by means of recursion is an acceptable method in compiler design.
Hence my interest that Forth have recursion.
*An extremely informative text on Forth, which I've been reading for the past
week.
________________________
Steve Solomon
solomon@aero.ARPA
"Wenn Forscher neben Forschern forschern, forschern Forscher Forscher."
________________________
Technical Details
Message-ID:
<187@aero.ARPA>Path:
arc1-ca.pugleaf.net!utzoo!linus!decvax!ittatc!dcdwest!sdcsvax!sdcrdcf!trwrb!trwrba!aero!solomonReferences:
<681@pucc-j>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site aero.ARPA Path: arc1-ca.pugleaf.net!utzoo!linus!decvax!ittatc!dcdwest!sdcsvax!sdcrdcf!trwrb!trwrba!aero!solomon From: solomon@aero.ARPA (Steve Solomon) Newsgroups: net.lang.forth Subject: Re: Virtues of Forth Message-ID: <187@aero.ARPA> Date: Mon, 6-Jan-86 15:01:18 EST Article-I.D.: aero.187 Posted: Mon Jan 6 15:01:18 1986 Date-Received: Wed, 8-Jan-86 07:32:39 EST References: <681@pucc-j> Reply-To: solomon@aero.UUCP (Steve Solomon) Organization: The Aerospace Corporation, El Segundo, CA Lines: 78 Keywords: AI, Recursion