Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

2025-01-25

Early programming

Way back, the municipal library had Technology books (classification P) on a little mezzanine floor, coincidentally together with English-language fiction. There were lots of interesting books there, like catalogues of all rolling stock on Swedish railways, books on Unmanned Aerial Vehicles, and also, I found, on Computer Science. At the time computers were semi-mythical beasts and programming languages had exotic and magical names (often ending in L for Language), so of course I was curious. The first book on programming I read was Ekman & Fröberg’s Lärobok i Algol (at the time of writing it was not necessary to disambiguate which version of Algol was meant). I can’t say that I understood the contents deeply, not least since I had no way of trying out the exercises—indeed I only got around to trying Algol-programming at uni many years later, and then in the form of Simula 67, designed as a superset of Algol 60. Actually, the book which really taught me programming was Kristel Siro’s Vi lär oss ADB och BASIC, and by then it was possible to get occasional access to an ABC 80 running BASIC, so I could truly try out writing programs. Incidentally, Siro’s book contained the Hailstone program (though without naming it) as well as Conway’s Life as programming exercises. I didn’t really get the point of either at the time, but have used them as test programs ever since.

Much later, a colleague was cleaning out their bookshelf, including their copy of Lärobok i Algol. I nabbed it and have kept it in my bookshelf as a memento. Reading it now it is noticeable how horridly badly the example code was written, but everybody was still learning at the time.

There is now a portable GNU Simula compiler which I have installed on my office laptop. Of course I have implemented the usual Hailstone program in Algol/Simula as well. When Algol was created, the designers defined a publication version of the language, which was meant to look good in scientific journals, as opposed to the caps-only, limited-character set hardware language. So, this is the publication version:

begin
  integer n;
  print("Input number: ");
  for n := read while n > 1 do begin
    n := if n / 2 = Entier(n / 2) then n / 2 else n × 3 + 1;
    print(n);
  end
end

2022-12-26

The patron saint

The reception traditions at the alma mater have most likely changed considerably since my undergrad days, but at the time there were certain rites and ceremonies that had to be performed for the recently admitted students to be accepted as full members of the school, the final being a party arranged by the new students for the rest of the section. Of course I ended up on the arranging committee. As the party would coincide with All Saints’ Day, we thought we should do something saint-themed, such as introducing the patron saint of programmers, whoever that might be. So I spent some time trying to get hold of a Catholic priest I had located in the phonebook (yes, it was all analog in those days). When I finally reached the priest, it turned out he belonged to the Liberal Catholic Church which had no truck with saints. Well, I had no idea! By then it was too late to start over with finding a Roman Catholic priest, so we had to drop the saints in favour of other ideas which I hope are safely forgotten by now.

Still, with the advent of the World Wide Web, it is now possible to search for all desired information, and one then finds that the inofficial patron saint of programmers is Isidore of Seville on account of him having compiled the first encyclopaedia, so here he is.

2017-05-08

Lua

I got to thinking about the design of programming languages. It may be a recency effect, but it seems to me there’s been a plethora of programming languages designed and supported by a single person in the last decade or so. Usually their existence is very clearly due to some pet peeve of the designer, which is reflected in the grammar and feature set of the language. Some, like Ruby, have syntactical features which, while I find them annoying, presumably save the designer some typing in their typical idioms.

I presume that older languages have gone through processes of standardisation that have worn off the most idiosyncratic bits, if they weren’t designed by a committee to begin with. (Which does not mean that there aren’t disgusting bits in those languages too.)

The latest of these vanity languages I’ve run in to is Lua, which, on the whole, is not too bad. I still haven’t decided if I like the concept of metatables, but I have to admit it allows some nifty and powerful tricks.

So, how does it do the usual Hailstone program?

function hailstone(n)
   while (n > 1) do
      n = (n % 2 == 0) and n // 2 or n * 3 + 1
      print(n)
   end
end

I was a bit disappointed that assignment isn’t an expression and thus couldn’t be fed into the print(). Of note is also that Lua does not adhere to the C syntax patterns used by Java, JavaScript, awk, or even Perl, in particular the conditional assignment is done through short-circuiting logical expressions instead.

2014-12-16

R

Prompted by a student project, I started looking into R today. Well, this is what the usual Hailstone program looks like in R:
hailstone <- function(n)
  while (print(n) > 1) 
    n <- if (n %% 2 == 0) n / 2 else 3 * n + 1; 
I started writing it in a very C-like way, but the functional nature of the language made itself increasingly more apparent as I played around with the code.

2014-07-05

Web-adapted

Now is the time for the JavaScript version of the Hailstone program:

<script>
  function hailstone(seed) {
    var output = "";
    for ( ; 
   output += seed, seed > 1 ; 
   seed = seed % 2 ? seed * 3 + 1 : seed / 2) { output += ", "; }
    return(output);
  }

  function submit(seed) {
    document.getElementById("output").textContent =
      hailstone(seed);
  }
</script>

<p>
  Seed: <input type="text" 
            id="seed" 
            onchange="submit(this.value)">
</p>
<p id="output"></p>

Put that into an HTML page and it generates the following box: (Try it, it actually works!)

Seed:

2011-05-06

I wouldn't want to write the parser

As in Algol, the programs are pretty-printed in different fonts, as in ABC 80 BASIC, the pretty-printing is enforced, as in COBOL, the programs pretend to be in English, as in Fortran, you have to use continuation characters, as in C++, the error messages are incomprehensible: AppleScript.

Here is our old friend, the Hailstone program:

on run
    display dialog "Seed" default answer "" buttons {"OK"} default button "OK"
    set a to (text returned of result) as integer
    set outstring to a as string
    repeat until a is equal to 1
        if a mod 2 is equal to 0 then
            set a to a div 2
        else
            set a to a * 3 + 1
        end if
        set outstring to outstring & ", " & a as string
    end repeat
    display dialog outstring buttons {"OK"} default button "OK"
end run

2010-12-06

Lazy!

I just ordered tickets through the web service of the Royal Dramatic Theatre and the mail receipt contains this mess:
Retrieve your booked tickets from the ticket office within ten calendar days, thereafter they will be automatically unbooked.

If your order is for a performance closer in time than nine days from the receipt of this message, the tickets must be retrieved at the latest three days before the performance.

You can also pay your tickets to our postal giro account and have the tickets sent to your home. Give your order number as reference. Note that it normally takes three working days for your payment to reach us.

[…]

Paid tickets are sent to the address you have given, if it is more than six days to the performance. If it is five days or less to the performance, they are in our ticket office.

Now, the web app already knows when I made my booking; on what day the performance is; and that I already paid for the tickets. So some web programmer couldn’t be arsed to write four lines of code to tell me directly that I have to get my tickets in the ticket office, since I booked the tickets on such short notice. Instead I have to work out the difference between calendar days, working days, and unspecified days, and possibly also get confused by whether Wednesday is two or three days before Friday if the performance is in the evening and if that is different if Thursday is a holiday even though they give performances then. That “postal giro” hasn’t been called that for five years just fits in with the rest of it.

A sternly worded letter has been sent.

2010-10-18

Geekiest punchline

Scenario:
Some coworkers are sitting around afternoon coffee. Newly employed D mentions having problems managing a website, the others speculate on what the cause may be and then the discussion turns to people’s favourite web programming languages.
A: “With all its faults, PHP is still pretty good.”
B: “Well, ASP with VBScript is better than its reputation, in particular the latest versions.”
C: “Rails, definitely!”
D [brightly]: “I’ve used CSS!”

2010-02-08

More hails

A much more recent pattern matching language is Perl, so for the sake of comparison, here is the Hailstone program in Perl:

for ($i = @ARGV[0];
print("$i\n"), $i > 1;
$i = $i % 2 ? $i * 3 + 1 : $i / 2) { };

(This is really a one-liner, but I've broken it over three lines to fit the text width.)

2010-01-27

Retrocomputing

I had planned to snark about the French burka law proposal, but I've had a bad day and can't be arsed, so instead I console myself (ha ha) playing with an ancient programming language.

SNOBOL is one of those languages developed in the 1960s when not all languages had to look like a clone of C and GOTO was not yet considered harmful. SNOBOL is a pattern matching language, which you can use for rewriting strings, much like what you'd do with, say awk, but without the benefit of loop statements or proper functions—all these are instead achieved through labels, each statement optionally containing a GOTO.

There are modern implementations of SNOBOL, so I downloaded one. It compiled nicely and as a test I wrote my favourite test application, the Hailstone program, even though it makes no use of the pattern matching functions. Viz:

I = INPUT
DO OUTPUT = I
EQ(I , 1) :S(END)
K = I / 2
EQ(2 * K , I) :S(EVEN) F(ODD)
EVEN I = K :(DO)
ODD I = 3 * I + 1 :(DO)
END


As you see, we have labels in the left column, the code in the middle, and then the GOTOs in the rightmost column, preceded by :. S indicates a conditional jump if the matching succeeded, F if the match failed. The rest should be pretty obvious.

2008-06-07

I foresee ergonomics issues

My first idea when I got the opportunity to work with the VPL DataGlove was to create a virtual keyboard. My plan was that it should be able to edit all aspects of the keyboard layout—the precise keys, where they were located in space and to have nifty functions to edit the layout so that one didn't necessarily have to lay out each key individually. To my disappointment the position sensors on the glove had neither the resolution nor speed necessary for typing, nor did the software handle collision detection well for so many objects, so when I realised that even a numerical keypad would require keys several centimetres across that could be pressed only once every few seconds or so, I gave up the idea.

Now I'm sorely tempted by the Virtual Laser Keyboard. Just the thing—a perfectly “soft” keyboard with optical tracking and wireless connection to your computer/PDA/mobile phone. Unfortunately the specs only say state that it has a QWERTY layout, so the risk is that the keyboard layout is locked in ROM, rather than being configurable, which would be a pity.

Then of course, there is the issue of typing on an unyielding surface, which brings down speed and accuracy, but I still could imagine trying it out.

2007-11-22

Buzzword of the year

I don't know if it is due to the onmarch of New Atheism, but suddenly “agnostic” is the word to use, though with regards to technology rather than the supernatural. What last year would have been “format-independent” is now presented as “format-agnostic”. I don't see that we thereby have gained in clarity.

To further confuse the issue, in many cases even “format-independent” would be a bit of marketing lies, as what in most cases is intended is “supports several different formats” where the magnitude of “several” is subject to the subjective opinion of the writer. A system could of course be format-independent in the sense that it is easily extended with user-supplied code to handle any new formats, very neat from a programming perspective, but usually not what someone buying a computer system expects.

2007-05-17

My TI-53



MarkCC has posted about innumeracy, to be specific, the misuse of terms in a mathematical context. This is of course something that is interesting in its own right—just the other day I heard a sales manager claim that sales of a product category had increased by 250%, rather than to 250% (of the index) and insisted it was correct even when I pointed out the error—but more importantly the discussion reminded me of my old Texas Instruments TI-53. I got it for my birthday, it must have been when I was in 9th grade. Pocket calculators were quite unusual at the time, but my father had a thing for gadgets and for once he had hit on something useful. This was a great toy. I read through the relatively thick but small (so it'd fit in the same box as the calculator) manual and soon had figured out how to program the calculator.

This was magic. I could easily compute functions and plot them on squared paper and got some kind of intuition for how they worked. I used my TI-53 almost all through high school, where it was invaluable, and with the constant use it eventually wore out, the keyboard sensors failing. It got an appropriate funeral: I opened it up to see what it looked like inside and then soldered off the surface-mounted processor. Eventually I sewed it to the breast of my student overall, where it still sits today.

2007-03-02

My god, it's full of CARs!

I laughed myself silly at this installment of xkcd:

2007-01-19

Erlang

One of the more obscure languages I've had reason to program in (indeed, have taken a course in) is Erlang. Erlang is a declarative real-time language, which owes much to Prolog and ML. It was originally developed in the 1980s as a more modern replacement for PLEX, but it never managed to topple the huge installed PLEX codebase, so Ericsson telephone exchanges are still programmed in PLEX, albeit a somewhat more developed and modernised version. On the other hand, Erlang is also still a living language with new releases coming out every few months.

Anyway, I was sent this old demo video, where Bjarne Däcker & Co show off the features of the new language. It's a marvellous document of its time, so if you were involved in CS research twenty years ago, watch and get nostalgic!