3 ms·
> you can rebuild with : { ; IMMEDIATE : } IMMEDIATE ; Does the call to DEPTH still work in that case? > But it is just meant to show that Forth is quite succ
by James_K 2y ago
> you can rebuild with : { ; IMMEDIATE : } IMMEDIATE ;
Does the call to DEPTH still work in that case?
> But it is just meant to show that Forth is quite succinct and powerful, which it does.
How does it show that? You could write a similar function in almost any programming language, with the only difference being the call to DEPTH which I don't see as being generally that useful. As an example in common lisp:
(defvar depth nil)
(defun { () (push (depth) depth))
(defun } () (unless (and depth (= (depth) (pop depth))) (error "Unbalanced expression")))
And this example doesn't stop working after a certain number of open braces. It's simultaneously more readable and more terse than the Forth code. Saying that Forth is powerful just because you can use it to write a compiler for a more powerful language is silly. By the virtue of Turing-completeness, you can do this for any language.
- veltas 2y ago> Does the call to DEPTH still work in that case? Code using the braces can use those definitions when you want to remove the overhead, and it disables the feature. This lets you use it as a programming aid, and then remove the overhead completely when it's not necessary or for a 'release build'. > It's simultaneously more readable and more terse than the Forth code. Does it still look neater when you use the syntax you've defined? > And this example doesn't stop working after a certain number of open braces. If you really want to remove the limit you can, Forth is just explicit about memory management. This is like C vs JavaScript. I'm quite happy playing with memory management myself, but if you don't like that or think declaring a fixed size stack is hacky then I can see why Forth wouldn't be appealing. Here is one way to remove the limit, it has drawbacks (but so do all dynamic memory approaches, they're just hidden by managed languages). : POP HERE -1 CELLS + @ -1 CELLS ALLOT ; : { DEPTH , ; : } DEPTH POP 1+ <> ABORT" Unbalanced" ; > Saying that Forth is powerful just because you can use it to write a compiler for a more powerful language is silly. Yes but in a few lines of simple code, and the result is actually a syntax that looks nice to use.
- James_K 2y ago> This is like C vs JavaScript. And you'll notice that C isn't a very powerful language. > Does it still look neater when you use the syntax you've defined? No because in CL you would define a with- macro that syntactic enforces equal nesting. So the CL version is: (defmacro {} (&rest args) `(progn ,@args)) I thought that would be too trivial demonstrate though. If you really don't like how it looks, you can fix it with two more lines of reader macros. The result would be a nicer syntax as you wouldn't have to put spaces around the braces. > Yes but in a few lines of simple code, and the result is actually a syntax that looks nice to use. Those are a few lines of highly complicated code that does something very simple and just happens to use the short function names { and }. The actual implementation of those functions is easier to write in almost any other language, which is what I was demonstrating with the CL example. If you want to make the case that Forth is powerful, you need to do more to prove it than show you can hack together a half-solution for a problem Forth created. Forth simply isn't a powerful language when counting matching braces is a difficult problem.
- veltas 2y ago> C isn't a very powerful language. Okay. > I thought that would be too trivial demonstrate though. If you really don't like how it looks, you can fix it with two more lines of reader macros. The result would be a nicer syntax as you wouldn't have to put spaces around the braces. The example is 'trivial' though. Also compare how big Forth is against CL, I mean Forths are often well under 10KB. I've written a Forth like this. I've never written a CL. Have you? > Those are a few lines of highly complicated code that does something very simple and just happens to use the short function names Just because you're incredulous doesn't mean that's not a feature. > easier to write in almost any other language, which is what I was demonstrating with the CL example CL isn't 'any other language' though, lisps specifically have quite powerful reflection. > hack together a half-solution for a problem Forth created Hacky half-solutions are charming, and Forth didn't create the 'problem' of structured syntax, it just didn't provide a solution out-of-box. That's one of the very explicit tradeoffs of the language.
- James_K 2y ago> Also compare how big Forth is against CL I would say a lot of this is because CL has a large standard library, but not much of that is primitive. Even then, I would say the CL standard lacks many things, so it is too small for general use rather than too large. From a perspective of implementation, I would almost always choose a small Scheme over Forth or something Forth-like. > CL isn't 'any other language' though, lisps specifically have quite powerful reflection. Those functions don't use reflection. > Forth didn't create the 'problem' of structured syntax, it just didn't provide a solution out-of-box. That's one of the very explicit tradeoffs of the language. It created the problem of not having structured syntax and that's a trade-off that you lose as soon as you write more than a maybe a hundred lines of code.