Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!ames!hc!lanl!unm-la!unmvax!nmtsun!caasnsr From: caasnsr@nmtsun.nmt.edu (Clifford Adams) Newsgroups: comp.lang.forth Subject: Re: Thinking FORTH Message-ID: <868@nmtsun.nmt.edu> Date: 3 Aug 88 00:21:48 GMT References: <8807291946.AA14510@jade.berkeley.edu> <1552@crete.cs.glasgow.ac.uk> Reply-To: caasnsr@nmtsun.nmt.edu (Clifford Adams) Organization: New Mexico Tech, Socorro NM Lines: 112 In article <1552@crete.cs.glasgow.ac.uk> orr%cs.glasgow.ac.uk@nss.ucl.ac.uk (Fraser Orr) writes: [much of article deleted] |:fact dup 0 gt else 1 then dup 1 sub fact if ; | |vs |int Fact ( n ) |{ | if ( n > 0 ) | return n*fact(n-1) | else | return 1 ; |} |If you find the former easier to understand, you've got a twisted |mind! (PS apoligies for my forth, it has been a while since I wrote |any, and after all it is not the most memorable syntax:^) Ahem. The sample of Forth code is not a realistic one. Try these comparisons. This is how Forth should be written, and how C is often written. Forth: ----- \ Factorial routine. Returns n! : factorial recursive ( n -- n! ) dup 0 > if dup 1- factorial * else 1 then ; C: -- /* factorial routine. Returns n! */ int factorial(n) {if(n>0) return(n*factorial(n-1));else return(1);} See! You can write C functions on a single line too! Now, which is more legible? >==Fraser Orr ( Dept C.S., Univ. Glasgow, Glasgow, G12 8QQ, UK) >UseNet: {uk}!cs.glasgow.ac.uk!orr JANET: orr@uk.ac.glasgow.cs >ARPANet(preferred xAtlantic): orr%cs.glasgow.ac.uk@nss.cs.ucl.ac.uk Now, I'll list some of the advantages and disadvatages I see in Forth. The following only apply to reasonably written Forth--it is possible to make a mess in almost any language. Advantages. ---------- 1. Forth is extremely compact, often more compact than reasonably written assembly language. Forth is usually more compact than the output of a compiler. 2. With appropriate hardware, Forth runs fast. Many MIPS fast, with gates on the order of 10,000. 3. Forth's compiler is simple. Because of this simplicity, Forth compiles very fast. Forth's compiler is also simple to change (just add IMMEDIATE words). 4. Forth's stack is a good place to store temporary results. This means fewer variables such as MISC and TEMP. 5. Forth can be easier for the final user to use. Compare the following, where the function rotate takes an angle and an appendage. Forth allows words to be defined which act as adjectives or adverbs. Forth: 90 left arm rotate C: rotate(90,left||arm) ; 6. Forth is usually run as an interpreter until the program is ready to be destination-compiled. Debugging (especially when working with hardware) can be much easier. In C, extensive test scaffolding is often needed because functions cannot be called interactively. Disadvantages. ------------- 1. Forth is seen as "strange" by much of the computing world. This does appear to be changing somewhat. 2. There is no true standard for Forth. This has hurt Forth in two main ways: A. No standard file system interface has been created. B. Floating point tools are non-standard. 3. No 32-bit standard (yet). 4. The usage of the stack often leads to stack-manipulating code breaking up higher-level parts of code. 5. Forth can die horribly from simple mistakes, like swapping a variable and a value before storing. 6. The hardware needed to run Forth most effeciently is not widely availible yet (I can't walk into a store and pick up a Forth machine). Well, that's enough for now. -- Clifford A. Adams --- "I understand only inasmuch as I become." ForthLisp Project Programmer (Goal: LISP interpreter in Forth) caasnsr@nmtsun.nmt.edu ...cmcl2!lanl!unm-la!unmvax!nmtsun!caasnsr