Which kid's beginners programming - Python or Forth?

Nick Craig-Wood nick at craig-wood.com
Thu Jun 30 13:30:13 CEST 2005


Rocco Moretti <roccomoretti at hotpop.com> wrote:
>  So for Math you'd do something like:
> 
>  y = b + mx + cx^2
> 
>  (Where ^2 is a superscript 2)
> 
>  For Python it would be:
> 
>  y = b + m*x + c*x**2
> 
>  IIRC, for Forth it would be something like (please excuse the mistakes 
>  in operator notation):
> 
>  x 2 ^ c * m x * + b + 'y' setvar

In FORTH you don't generally use variables unless you really have to -
that is what the stack is for, so you'd write a word like this...

variable c 10 c !
variable m -2 m !
variable b 14 b !

: quad ( x -- b + m*x + c*x**2 )
  dup dup ( x x x )
  * c @ * swap ( cx**2 x )
  m @ * + ( m*x + c*x**2 )
  b @ + ( b + m*x + c*x**2 )
;

And now we test

  7 quad . 490  ok

Was that easy?  Not really! Compared to python...

>>> c = 10 
>>> m = -2
>>> b = 14
>>> def quad(x): return b + m*x + c*x**2
... 
>>> quad(7)
490

Was it fun?  Well yes it was! FORTH is much lower level than python
and you learn different things from it.  At each step you have to
worry about what is on the stack which attention to detail is
important for a programmer. Its a lot of work to do even the simple
stuff though.

Its much easier to understand how FORTH works, and even implement your
own from scratch.

I learnt FORTH a long time ago, and I haven't used it for many many
years! Its major pull back then was that it was fast, and easier to
write than assembler.  I don't think that really matters now though,
Python is just as fast thanks to the 3 GHz machine I'm running it on
(rather than the 4 MHz one I ran FORTH on then!)  I think FORTH would
be an interesting supplimentary language for anyone to learn though...

*However* I reckon Python would make a much better first language than
FORTH. The batteries included approach make a young programmers life
much, much more fun, rather than starting from almost nothing (in
modern terms) with FORTH.

And like FORTH, Python has the interactive console which is essential
when starting out.

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list