
Advanced Forth Programming Topics - eatonphil
http://www.mosaic-industries.com/embedded-systems/sbc-single-board-computers/freescale-hcs12-9s12-c-language/instrument-control/forth-language-program-development
======
snaky
One of the most interesting modern concatenative languages - Kitten
([http://kittenlang.org/](http://kittenlang.org/))

~~~
groovy2shoes
Agreed! I haven't had so much learning a new language in a long time. Lots of
cutting-edge stuff going on in Kitten, and all the concatenative goodness you
hear about from Factor fans, but (imo) in a much more palatable package. Part
of what makes it so palatable for me is that there have been some syntactic
compromises rather than relentless emphasis on RPN. For example, rather than
(as in Forth):

    
    
        0 = IF foo ELSE bar THEN
    

Kitten allows you to say:

    
    
        -> x;
        if (x = 0):
          foo
        else:
          bar
    

which I personally find much easier to follow.

So, you get local variables, infix expressions, and some amount of non-RPN
syntax, without sacrificing any of the power you'd get from another
concatenative language.

I've talked to Jon (the creator) a bit off-and-on, and from what I gather
there are some _really_ neat things being planned for Kitten in the near
future :)

~~~
tluyben2
> which I personally find much easier to follow.

Think that's right. I've been doing a lot of oK, J, Pyth, Forth (working on my
own for Z80) for fun and I find it actually very easy to read terse code after
a few months. So much that the way you (and most) prefer actually annoys me.
It's so much space taken in my field of vision while with terse code I can
read entire, quite full and complex programs on 1 screen without scrolling.

Don't worry; I would not say that this is a good idea for programming in teams
or maintainable software. I think it is if you have a group of like minded
people who prefer this. For me personally however, it makes me very productive
and I use the above languages for prototyping now as I get stuff done quicker
and with less bugs. I think it's very personal though.

Hooray for kitten lang by the way.

~~~
groovy2shoes
For me, the lack of readability isn't necessarily about the terseness, _per
se_. It's about trying to keep track of the stack in my head at all times, and
the "unnatural" order that the "keywords" appear in.

    
    
        <condition> IF <consequent> ELSE <alternative> THEN
    

is something I _still_ struggle with, even after studying forth (via Leo
Brodie's "Starting Forth" and "Thinking Forth") for about 3 months. Something
like this I have no trouble with:

    
    
        IF <condition> THEN <consequent> ELSE <alternative>
    

but the addition of the (optional) local variables helps me to keep track of
the stack more easily (if you could even consider it the stack at that point).

In general, I don't have much trouble with RPN, but a few of the constructs in
forth have eluded me...

~~~
dragonwriter
Yeah, I think the choice of keywords in that case isn't great for the
structural order that they have to appear in

    
    
      <condition> THEN <consequent> ELSE <alternative> IF 
    

would probably be a better order, in the same structure, keeping the same
keywords.

~~~
groovy2shoes
That's surprisingly _much_ easier for me. I don't know why, but the other
order just throws me for a loop every time, and I find myself carefully re-
reading the same code multiple times. However, given the kind of
metaprogramming power you get in Forth, I don't see why a more straightforward
production couldn't be used in this case.

------
i_feel_great
Which Forth has a "good" standard library?

~~~
undecidabot
Not Forth, but Factor ([https://factorcode.org/](https://factorcode.org/)) is
very much inspired by Forth (also being a stack-based / concatenative
programming language), and it does have a good standard library complete with
your usual data structures along with extra libraries for regexp, several
parsers (csv, json, xml), an http server and client, a web framework (built on
top of that server with SQL DB drivers), a ui framework (via OpenGL), and even
xkcd ([http://docs.factorcode.org/content/article-vocab-
index.html](http://docs.factorcode.org/content/article-vocab-index.html)).

Check this blog out for some cool things you could do with Factor: [http://re-
factor.blogspot.com/](http://re-factor.blogspot.com/)

~~~
AstroJetson
Factor is cool, but it has some differences that may trip an experienced user.
But for a first time Forth user, the extra frameworks/libraries may be a help.

As pointed out below, FIG Forth has a great deal of support.

All my interactions using Forth have been on underpowered systems pounding on
the bare metal. So lots of the words created supported that environment. Math
words and geometry words came from FIG Forth.

------
akavel
Is anyone aware of any Forth or Forth-like language for JVM or (ideally)
Dalvik? Ideally, with a REPL allowing to access all Android APIs and create
callbacks/interface implementations... super-ideally, statically-typed... eh,
one can dream. By the way, a ML-like would fit me even more. (Or a statically-
typed LISP.)

------
jevinskie
Does anyone have links to resources that speak to converting a stack machine
(Forth, in my case) into SSA form (LLVM, in my case)? My first approach was to
just subroutine thread everything and see how well the inliner and optimizer
handles that.

~~~
jevinskie
I did some more digging and it looks like I should read the RAFTS paper. [0]

[0]:
[http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.5.38...](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.5.3854)

------
dewster
IMO, anytime the programmer has to micro-manage anything it's basically
assembly (Forth forces you to micro-manage the stack).

I dislike syntax which uses the <> symbols, but I'm all for RPN and postfix
(in theory anyway).

~~~
jononor
Does imperative programming force you to micro-manage variables and arguments?

For me I think the challenge is that the stack-effects are 'hidden' in the
code. However, this seems like it could be solvable by compiler (typed stack
effects for instance) and tooling/IDE.

~~~
dewster
> Does imperative programming force you to micro-manage variables and
> arguments?

I just don't think many programmers fully take into account all the trouble a
stack-based language is. You're constantly rearranging the stack to get values
stored on it in position to call functions and such. You know stack
manipulations are inherently wasteful, and you worry about doing too many of
them. So you end up micro-managing them, and often end up sacrificing
readability for efficiency. And the readability of stack languages is already
woefully bad. I don't really understand why they continue to exist.

------
anta40
Is there any open source, cross platform Forth compiler which able to create
native executables like GCC/FPC do?

~~~
i_feel_great
gForth and Factor (heavily Forth-inspired) appear to do just that.

~~~
larsbrinkhoff
I don't see that Gforth can create executables.

It can create image files, but those are not directly executable; they need to
be loaded by the Gforth runtime.

