Live data from Hacker News

Easy Forth (2015)

skilldrick.github.io

81–90 of 128 posts

Re: Easy Forth (2015)

#81
post #73

Earlier quoted context omitted.

Well, to bake an apple pie from scratch, you must first create the universe. In any programming language, to read an arbitrarily long line into memory, you need an arbitrarily large computer, so your software may need to pause to convert more Temu orders, continents, asteroids, or star systems into computronium. If you're not willing to go that far, you have basically two choices: 1. Process the line in a streaming f…

The point is, I don't want to spend lots of time solving these essential problems, when I actually want to learn the language through solving puzzles. It seems, that Forth does not lend itself to be learned that way, since even very basic things are not provided and require in-depth knowledge of Forth and developing manual memory managed solutions to problems, that are solved in almost every programming language in t…

Forth was sort of designed by and for people who did want to solve these essential problems anew for each application. Chuck Moore claimed many times that a tailored ("ad hoc") solution that solves only the part of the problem you need to solve for a particular application would be 10× smaller and simpler than a generalized solution that has to balance the needs of all possible applications. He considered it preferable to not have a lot of library code in your application to solve problems you don't actually have. Maybe your ad-hoc solution is brittle, but it's brittle precisely in ways you know about, not in ways you don't.

But you don't have to use Forth that way just because Chuck did. You can totally use a generalized string library in Forth. I don't know which one to recommend, but http://turboforth.net/resources/string_library.html seems to be one possibility.

You can be sure that Python's file.readlines()† will have trouble if you try to read a line that is much longer than your RAM size.

You can get pretty far with just built-in standard functionality, though:

    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
    128 constant len  create buf len allot  ok
    : greet ." Name? "  buf len accept  ." Hello, " buf swap type ." !" ;  ok
    greet Name? Zelphir Hello, Zelphir! ok
And, as you said, GForth comes with a heap-allocated string library https://gforth.org/manual/String-words.html#String-words which you can use if you first say

    include string.fs
______

† ever since Python 2.0, I'd recommend using list(file) instead of file.readlines(), or just iterate over the file directly, like [line.strip() for line in file if line.startswith('zel')]

Re: Easy Forth (2015)

#82
post #19
post #7

Earlier quoted context omitted.

Right. And once again, you’ll also notice that no one is actually coding anything useful in Forth.

As I like to say: "C is a language that solves a million problems. Forth is a million languages that solve almost nothing." :-P I've been reading about Forth for 30-40 years. The dual stack is easy to understand. My problem is that I cannot see how control flow works in Forth, e.g. a simple if-then-else. I think that something as fundamental as an if-then-else should be obvious in a useful language. Heck, it's obviou…

> My problem is that I cannot see how control flow works in Forth, e.g. a simple if-then-else.

figuring this out for my own FORTH interpreter was a moment i still remember, nearly 50 years later. quite a revelation

Re: Easy Forth (2015)

#83
post #78

Earlier quoted context omitted.

BASIC, especially the Microsoft dialect, became the dominant language for microcomputers because it would fit in a tiny space, e.g. 4k. For that matter it was big in the minicomputer age because it was used in multitasking systems that weren't that big. Circa 1980 my high school had a PDP-8 which had three terminals and could run a three user BASIC with just 32k 12 bit words. There weren't a lot of languages which wo…

Original BASIC did not fit into tiny space, that is why while Dartmouth BASIC always compiled into machine code before execution, everyone that learnt BASIC in 8 bit systems thinks it was originally interpreted and compilers only came later, which was the compromise to make it fit into a few KB. Jupiter ACE had its followers, and it was common to see ads on Your Sinclair and similar magazines for ROM replacements usi…

That PDP-8 BASIC was a miracle of shoehorning as was everything else on the PDP-8.

My favorite minicomputer BASIC that I got to use was on RSTS/E on the PDP-11 which had split 64k address spaces for code and data and used fairly advanced compilation techniques. Roughly the RSTS/E experience was like having your own Apple ][ but with a hard drive and a little more oomph. I grew up in New Hampshire right next door to DEC's headquarters in Massachusetts and there were DEC minicomputers everywhere.

Microsoft had a compiled BASIC (like run a compiler, not compile interactively like Microware's BASIC09) on CP/M for the Z-80 which was a much better compiler target than the popular 6502.

I wrote a FORTH for the TRS-80 Color Computer using the OS-9 operating system which had maybe 2000-3000 lines of assembly code. FORTHs at the time often did block I/O directly to the disk but OS-9 had an API to access files that was pretty similar to Unix and my FORTH exposed that.

Re: Easy Forth (2015)

#84
post #54
post #49

Earlier quoted context omitted.

As @addaon writes, your missing ingredient is immediateness. This is one of the most powerful, yet mind-boggling aspects of Forth. I encourage you to check it out, it will make you grow as a developer.

I will definitely look into that. If understanding this special IMMEDIATE mode is required to understand the Forth interpreter for something as fundamental as control-flow, it seems fair to say that Forth is not a simple language. It's not just an advanced programmable RPN calculator An RPN calculator has a program counter, which makes control-flow easy to understand. In comparison, C is a high level language, but th…

Very much the contrary! In C all the syntax and control structures have to be built into the language; this makes C a much more complex language than Forth, because in Forth the language and interpreter don't even have to support things like comments, string literals, variables, and control flow. Because of immediate words, all of that can be built on top of the base language in high-level Forth, and almost always is. This allows the language itself to be enormously simpler.

It's also generally the case that in a native-code-compiling Forth the mapping from the Forth source to the machine code emitted is very much simpler and more direct than in C; as Virgil implicitly pointed out, the machine code is generally more or less in the same order as the source code, which in C it is not, and you don't have a bunch of implicit type conversions, ad-hoc polymorphic arithmetic operators, and so on. (It doesn't have to be more direct, since you can do arbitrary computation at compile time, but it usually is.)

Re: Easy Forth (2015)

#85
post #5

Glad to see Forth on HN today! For anyone who likes playing with small experimental projects, I once made a minimal, esoteric canvas colouring language inspired by Forth and Tixy: https://susam.net/fxyt.html

This is fun!

I was going to try to draw a circle but was missing sin/sqrt. Then I thought of using a lookup table but got stumped.

Do you have any pointers for drawing a circle?

I'm looking at demo #4 (https://susam.net/fxyt.html#XYpTN1srN255pTN1sqD) to see where the circular shapes are coming from.

Have you seen Forth Haiku? https://forthsalon.appspot.com/

Re: Easy Forth (2015)

#86
post #19

Earlier quoted context omitted.

As I like to say: "C is a language that solves a million problems. Forth is a million languages that solve almost nothing." :-P I've been reading about Forth for 30-40 years. The dual stack is easy to understand. My problem is that I cannot see how control flow works in Forth, e.g. a simple if-then-else. I think that something as fundamental as an if-then-else should be obvious in a useful language. Heck, it's obviou…

A million times this. The syntax is not difficult to see, but the action seems mysterious. The article gives an example > : buzz? 5 mod 0 = IF ." Buzz" THEN ; Seems to work okay. What about immediate mode? > 10 5 mod 0 = IF ." Buzz" THEN ; action is not a function Well, I guess that does it for me. Factor, another stack-based languages, has a more legible version of this, where you can push an anonymous lambda onto t…

Forth control structures don't work in interpret state because control structures involve executing code out of order: jumping from the end of a loop back to its beginning, etc. In interpret state there's nowhere to jump to. So you have to put control structures inside a colon definition for them to work. Also true of strings in traditional Forths, but GForth just dynamically allocates some memory and leaks it instead.

Re: Easy Forth (2015)

#87
post #7

Earlier quoted context omitted.

Right. And once again, you’ll also notice that no one is actually coding anything useful in Forth.

Bitcoin’s scripting / smart contracting language is Forth. Were there anything in the crypto space is actually solving a problem is up to your own biases and prejudices. But if you pick one thing as actually trying to solve a real problem, payments over lightning is probably that. Lightning, at its core, is a state machine composed of Forth spend scripts.

No, Bitcoin Script is not Forth. Bitcoin Script is designed to guarantee termination, so no Turing-complete programming language was permitted. Like BPF, Bitcoin Script doesn't have subroutine definitions or backward jumps, so each opcode executes at most once.

This is like saying JSON is C++.

Re: Easy Forth (2015)

#88
post #23

Earlier quoted context omitted.

It's a cool little niche language. If you're neither interested in the coolness, nor its little niche - there's no need to be dismissive.

Yes, my comment came across a bit harsh, and it’s fine to pick up a few negative karma points. But I keep seeing Forth posts every two weeks where everyone has just built yet another interpreter. Actually I did a few projects with Forth and I find it very cool: [0] https://github.com/s-macke/Forthly [1] https://github.com/s-macke/starflight-reverse [2] https://s-macke.github.io/concepts-of-programming-languages/...

Heh, I see you linked StoneKnifeForth there!

Re: Easy Forth (2015)

#89
post #85
post #5

Glad to see Forth on HN today! For anyone who likes playing with small experimental projects, I once made a minimal, esoteric canvas colouring language inspired by Forth and Tixy: https://susam.net/fxyt.html

This is fun! I was going to try to draw a circle but was missing sin/sqrt. Then I thought of using a lookup table but got stumped. Do you have any pointers for drawing a circle? I'm looking at demo #4 ( https://susam.net/fxyt.html#XYpTN1srN255pTN1sqD ) to see where the circular shapes are coming from. Have you seen Forth Haiku? https://forthsalon.appspot.com/

doesn't need to be accurate, could do something like this (distance + treshold) :

https://susam.net/fxyt.html#XN128dXN128dpYN128dYN128dpsN4096...

smaller with dup :

https://susam.net/fxyt.html#XN128dDpYN128dDpsN4096lN255pC

circles pattern : https://susam.net/fxyt.html#XDpYDpsN8qN3riN255pC

PS: fun tool !

Re: Easy Forth (2015)

#90
post #87

Earlier quoted context omitted.

Bitcoin’s scripting / smart contracting language is Forth. Were there anything in the crypto space is actually solving a problem is up to your own biases and prejudices. But if you pick one thing as actually trying to solve a real problem, payments over lightning is probably that. Lightning, at its core, is a state machine composed of Forth spend scripts.

No, Bitcoin Script is not Forth. Bitcoin Script is designed to guarantee termination, so no Turing-complete programming language was permitted. Like BPF, Bitcoin Script doesn't have subroutine definitions or backward jumps, so each opcode executes at most once. This is like saying JSON is C++.

It's more like saying JSON is Javascript, which it is to a degree.
Post reply on HN