Functions as parameters in FORTH

I was looking for something like the lambda in FORTH. It turns out there is the single quote operator which puts the “execution token” of a word on the stack, which you can EXECUTE later.

To give a practical example of such use, here is a maparray function:

: maparray ( xt a- n -- )
    begin
        dup 0 >
    while
            -rot ( n1 xt a- )
            dup @ ( n1 xt a- n2 )
            swap ( n1 xt n2 a- )
            -rot ( n1 a- xt n2 )
            swap ( n1 a- n2 xt )
            dup >r ( n1 a- n2 xt, xt )
            execute ( n1 a- n3, xt )
            over ( n1 a- n3 a-, xt )
            >r ( n1 a- n3, xt a- )
            swap ( n1 n3 a-, xt a- )
            ! ( n1, xt a- )
            1 - ( n4, xt a- )
            r> ( n4 a-, xt )
            cell + ( n4 a2-, xt )
            swap ( a2- n4, xt )
            r> ( a2- n4 xt )
            -rot ( xt a2- n4 )
    repeat
    drop drop drop
;

With this function, I can pass in the execution token, an array address, and the length of the array (in cells) and the execution token is executed on each array element, with the results stored back in the array.

christopher@nightshade ~/Repos/forth-learning$ gforth func.fs
Gforth 0.7.3, Copyright (C) 1995-2008 Free Software Foundation, Inc.
Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
Type `bye' to exit
create a 1 , 2 , 3 , 4 , 5 , .s <0>  ok
a 5 cells dump                 
7FCF3CC56378: 01 00 00 00  00 00 00 00 - 02 00 00 00  00 00 00 00  ................
7FCF3CC56388: 03 00 00 00  00 00 00 00 - 04 00 00 00  00 00 00 00  ................
7FCF3CC56398: 05 00 00 00  00 00 00 00 -                           ........
 ok
: foo 1 + ;  ok                
' foo a 5 maparray  ok         
a 5 cells dump                 
7FCF3CC56378: 02 00 00 00  00 00 00 00 - 03 00 00 00  00 00 00 00  ................
7FCF3CC56388: 04 00 00 00  00 00 00 00 - 05 00 00 00  00 00 00 00  ................
7FCF3CC56398: 06 00 00 00  00 00 00 00 -                           ........
 ok

So, basically, the maparray function takes a function that acts on one element, and foo here is a function that adds one to an element, and I pass foo into maparray.

Advertisement

2 thoughts on “Functions as parameters in FORTH”

  1. Hi Christopher,
    I played with Forth some months ago while reading “Starting Forth” (or was it “Thinking Forth”?) by Leo Brodie. Very interesting language. I read that the Chuck Moore’s company sells hardware to leverage Forth features? Really cool.
    Hope to see a Forth on Arduino experiment in your newsletter soon haha!

    Like

  2. May I make a suggestion? You have done all that within one single word that does a lot of stack manipulation. Would you like to try breaking that down into several word definitions, even though some of those would only be used once?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s