Explain concatenative languages to me like I'm an 8-year-old

后端 未结 7 2075
粉色の甜心
粉色の甜心 2021-01-30 22:53

I\'ve read the Wikipedia article on concatenative languages, and I am now more confused than I was when I started. :-)

What is a concatenative language in stupid people

7条回答
  •  Happy的楠姐
    2021-01-30 23:03

    After reading http://concatenative.org/wiki/view/Concatenative%20language and drawing on what little I remember of fiddling around with Forth as a teenager, I believe that the key thing about concatenative programming has to do with:

    • viewing data in terms of values on a specific data stack
    • and functions manipulating stuff in terms of popping/pushing values on the same the data stack

    Check out these quotes from the above webpage:

    There are two terms that get thrown around, stack language and concatenative language. Both define similar but not equal classes of languages. For the most part though, they are identical.

    Most languages in widespread use today are applicative languages: the central construct in the language is some form of function call, where a function is applied to a set of parameters, where each parameter is itself the result of a function call, the name of a variable, or a constant. In stack languages, a function call is made by simply writing the name of the function; the parameters are implicit, and they have to already be on the stack when the call is made. The result of the function call (if any) is then left on the stack after the function returns, for the next function to consume, and so on. Because functions are invoked simply by mentioning their name without any additional syntax, Forth and Factor refer to functions as "words", because in the syntax they really are just words.

    This is in contrast to applicative languages that apply their functions directly to specific variables.

    Example: adding two numbers.

    Applicative language:

    int foo(int a, int b)
    {
        return a + b;
    }
    
    var c = 4;
    var d = 3;
    var g = foo(c,d);
    

    Concatenative language (I made it up, supposed to be similar to Forth... ;) )

    push 4
    push 3
    +
    pop
    

    While I don't think concatenative language = stack language, as the authors point out above, it seems similar.

提交回复
热议问题