Hacker News new | ask | show | jobs
by ecnahc515 2254 days ago
This is pretty cool. After reading half the examples, I started wondering "wait what are you doing with the output variable", and then it clicked and I checked the docs, and yep, outputting to stdout is done by assignment to a magic `output` variable. A very interesting/novel approach to things.
4 comments

Haskell is somewhat similar, too! I think some of this comes from C.

The goal of a Haskell compiler is to take your code and spit out an executable. That executable is in fact the value named “Main.main,” which must be of the in-Haskell type (translating to English) “a program which produces nothing.” In the source code you are constantly juggling values of type “a program which produces ____” and linking them together with the in-built methods, to get to this point, and the compiler just takes one of these things, based on a naming convention, and spits it out to the filesystem. And I think it got this convention because it was really handy how C similarly used a well-known name to specify its entry point into the source file.

The difference is then that Pointless does not have a type restriction on what “main” can be and instead of “saving to the filesystem” it dumps to stdout. Presumably with the right sort of metaprogramming attitude one could do what Haskell does in Pointless, maybe—when output is a program it could maybe spit out a compiled executable to stdout and you would redirect it to a disk location and set its executable permission manually.

Actually `main` in Haskell can be an IO action returning any type, eg this is a perfectly valid main:

    main :: IO Int
    main = print "10" >> pure 10
the return value is just ignored when it's actually invoked as an executable (but can be used if invoked eg. in the repl, or if main is also called from somewhere deeper in the code).
Classic BASIC implementation picked up on this idea. For instance some dialects had an INKEY$ variable which looks like a string, but magically reads console input.

GNU Bash has a magic variable RANDOM that produces random numbers. Assignments to it appear to be ignored.

In Algol, the name of the enclosing function acts as a variable to which you assign in order to produce the return value. For instance, see the assignment to B in the nested function here;

https://en.wikipedia.org/wiki/Man_or_boy_test

> GNU Bash has a magic variable RANDOM that produces random numbers. Assignments to it appear to be ignored.

Assignments are used to seed¹ the random number generator². This isn't limited to just bash/zsh/etc, it is intrinsic to the process of generating random numbers. Being aware of this is useful as it poses a significant footgun. The /dev/random docs³ provide a nice overview of how it is often handled on various systems at the OS level to deal with reboots.

An example of the fun that this can produce can be seen in a Debian bug⁴, running a script that uses subshells and RANDOM with bash will behave very differently when using zsh. In this instance you have to hope that subshells aren't being used to create unique logs as an example.

1. https://en.m.wikipedia.org/wiki/Random_seed

2. https://en.m.wikipedia.org/wiki/Random_number_generator

3. https://en.m.wikipedia.org/wiki//dev/random

4. https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=828180

I remember from GW-BASIC there being, ERR, ERL, CSRLIN, and probably a few others. C64 BASIC V2.0 had ST and TI/TI$.

BASIC's version of $RANDOM is RND(x), and I remember that the sign of the argument was the most important thing. Negative values seeded the PRNG and any positive argument got you the next random number. Some later dialects had a RANDOMIZE statement.

The funny thing is BASIC also had a couple functions that took input variables, but didn't do anything with them, like POS() and FRE(). I think FRE's argument may have been significant on a couple BASICs.

Flashback! 7 year old me named my rabbit after the inkey variable.
Anyone remember the SNOBOL set of languages?
I used it in CS grad school in the early 70's. I have both books on it, including "The Macro Implementation of SNOBOL4."

I used it to write a go-moku (5-in-a-row) playing program in an AI course and got an A when it beat the professor.

Familiar but without hands-on experience. Read some reference manuals on it and code samples.

I recognized right away that the span and break pattern-matching operators of Snobol must be the inspiration behind C library names like strspn and strpbrk.

When I was in high school, I stumbled across the source code for cc on the UIC mainframe. Part of it was in either Snobol or Spitbol (I don't remember now) and I remember going to the library to get a book on the language to learn enough to be able to decipher the code.
Actually, something like this has been routinely done at the low level, especially when programming in assembly. (Also, trivially done in C++.)