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

后端 未结 7 2012
粉色の甜心
粉色の甜心 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条回答
  •  面向向阳花
    2021-01-30 23:16

    My pragmatic (and subjective) definition for concatenative programming (now, you can avoid read the rest of it):

    -> function composition in extreme ways (with Reverse Polish notation (RPN) syntax):

    ( Forth code )
    : fib
      dup 2 <= if
        drop 1
      else
        dup 1 - recurse
        swap 2 - recurse +
      then ;
    

    -> everything is a function, or at least, can be a function:

    ( Forth code )
    : 1 1 ; \ define a function 1 to push the literal number 1 on stack
    

    -> arguments are passed implicitly over functions (ok, it seems to be a definition for tacit-programming), but, this in Forth:

    a b c
    

    may be in Lisp:

    (c a b)
    (c (b a))
    (c (b (a)))
    

    so, it's easy to generate ambiguous code... you can write definitions that push the xt (execution token) on stack and define a small alias for 'execute':

    ( Forth code )
    : <- execute ; \ apply function
    

    so, you'll get:

    a b c <- \ Lisp: (c a b)
    a b <- c <- \ Lisp: (c (b a))
    a <- b <- c <- \ Lisp: (c (b (a)))
    

提交回复
热议问题