Rick Collins has been struggling to figure out how to print hex numbers since at least 2015.
We had this example of using a flag as a mask was given (modified slightly here to make it ANS-Forth):
: >CHAR ( [0,15] -- char )
DUP 9 > 7 AND + [CHAR] 0 + ;
A Forther (Hans Bezemer) complained that this code is bad style,
and the Forth-200x committee-member Bernd Payson defended it:
On Sunday, May 24, 2015 at 5:21:05 PM UTC-7, Bernd Paysan wrote:
> Hans Bezemer wrote:
> > ...True, it
> > is a clever piece of programming, but in our opinion it is bad
> > style. Why? Because you are using a flag as a bitmask, which is a
> > completely different datatype. Although there is no such thing as
> > “data typing” in Forth, this way of programming makes it
> > difficult to understand and maintain a program, which the
> > ANS-Forth standard acknowledges:
>
> Honestly, this is only a problem if you don't know that idiom. A lot of
> people don't know that you can use masks and logical operations, so they
> will find that operation strange.
> ...
> Therefore, I would reiterate what I told Hugh some times: If you don't know
> something, it's *not* the fault of the person who uses and knows that thing.
> It's entirely your fault, and if you want to be a wise person, you'd rather
> learn it that complain.
>
> IMHO the whole datatype based thinking about cells in Forth is ill-advised.
> First, and foremost, a cell is a bit pattern, and if you add and subtract
> it, it's a mod 2^n ring. You can use that to some degrees as integer, but
> it's not an integer. It's a cell.
>
> --
> Bernd Paysan
> "If you want it done right, you have to do it yourself"
Elizabeth Rather responded:
> Thank, you, Bernd, well-said.
>
> Cheers,
> Elizabeth
A little later we had this:
On Tuesday, May 26, 2015 at 11:37:37 AM UTC-7, Elizabeth D. Rather wrote:
> On 5/26/15 8:15 AM, rickman wrote:
> > On 5/26/2015 1:20 PM, WJ wrote:
> >> That code is unreadable and unmaintainable. It's very hard
> >> to figure out how it accomplishes its task. I would not
> >> pay a programmer to produce code like that.
> >>
> >> This is somewhat more understandable:
> >>
> >> : >char
> >> dup 9 > if [char] A 10 - else [char] 0 then
> >> + ;
> >
> > If you have any reason to look at the definition of >char then I can't
> > see how you would not understand how it works. Perhaps this would be a
> > better definition...
> >
> > \ Convert n to ASCII char
> > : >CHAR ( n -- char ) DUP 9 > 7 AND + ASCII 0 + ;
> >
> > Lol, saying it is unreadable and unmaintainable is a bit of a stretch. I
> > think that exact code has been read and understood as well as maintained
> > for many years now. I am pretty sure I have seen something similar to
> > it way back when I was learning assembly language. I think it took me
> > two minutes to see what it was doing.
>
> Indeed, proper documentation as in rickman's version above is essential
> to readable & maintainable code. Neither of the previous versions is
> acceptable, although I find the shorter code quite clear (and, as
> rickman notes, it's been around for many years). I would have preferred
> [CHAR] instead of ASCII, however, as it's Standard.
>
> Cheers,
> Elizabeth
It is not really that I (Hugh Aguilar, slammed by Bernd Payson for being unwise) don't know how to
use a flag as a mask in logic-arithmetic --- it is just that I don't want to --- this is my own code
(written in ANS-Forth, and one of the previous versions mentioned above as not being acceptable):
: indexed-char \ index adr cnt -- char \ the string will be used as an array of chars
rover 0< abort" *** INDEXED-CHAR given a negative index ***"
rover <= abort" *** INDEXED-CHAR given an index too large ***"
+ c@ ;
: hexit>char \ [0,15] -- char
s" 0123456789ABCDEF" indexed-char ;
This is an example of how I relate to Forth differently from most ANS-Forth and Forth-200x enthusiasts
(I'm one of the few people on the planet who has had a paying job writing Forth code).
I consider >CHAR above to be tricky code (the 7 is a "magic number" that is only going to be meaningful
to somebody who has memorized the ascii character set) --- these kinds of tricks were done
in the 1970s when programmers' primary goal was to save memory.