Friday, March 9, 2007

Part Isa - Introduction, Manners & 'Hello, world'_

A good programmer constantly strives to learn new techniques, better ways of building a mouse-trap. Anyone who is happy to stick with the tools that they know is likely to become stagnant and bored. There are many in the IT industry who I would call 'band-wagon hoppers' who are always looking to jump from one latest fad to the next. Java one day, Ruby the next, Haskell on Saturday, Groovy on Sunday.

A language that has stood the test of time is INTERCAL, created by Donald Woods and James Lyon in 1972. As a programming language, INTERCAL remains every bit as useful as it was over thirty years ago.

Despite its longevity, INTERCAL has never seen the peaks of popularity that more 'mainstream' languages like C, C++, Java and even Visual Basic have experienced. (This is despite it being superior to Visual Basic in nearly every respect.) But in the same way that learning a functional language like Haskell can make you a better Java programmer, I suggest that you learn at least some rudimentary INTERCAL to expand your field of knowledge of alternative programming techniques.

For my examples, I will be using the C-INTERCAL compiler which can be downloaded on the official site. To build the compiler you will need to jump through the standard './configure' and 'make' hoops. (Microsoft Windows users should upgrade to Linux or Mac OS X before completing this step.)

After building the compiler, create a new file called 'hello.i' with the following contents:

DO ,1 <- #1
DO ,1SUB#1 <- #234

I don't expect you to understand this code at the moment; all will be explained in time. Let's just attempt to compile this file using the command 'ick hello.i'. You should receive the following output:


Taken from the revised INTERCAL manual: "INTERCAL was inspired by one ambition: to have a compiler language which has nothing at all in common with any other major language."

Already we have encountered one way that INTERCAL differs from other languages - it doesn't like being bossed around. You can't just tell it to 'DO, DO, DO'. If you want it to respond nicely you will have to say 'PLEASE' at least 1/5th of the time. However INTERCAL will not stand for brown-nosing either, so don't say PLEASE more than 1/3rd of the time. To assist in INTERCAL programming there is an intercal.el Emacs mode included with the distribution that randomly expands 'DO ' to 'PLEASE DO ' 1/4th of the time. Apart from good manners, there is no semantic difference between 'DO', 'PLEASE DO' or simply 'PLEASE'.

So correcting our source:

DO ,1 <- #1
DO ,1SUB#1 <- #234

:we will then be able to compile with no error(2). If you run your compiled program, you will find that it outputs the single letter 'h' then exits. Congratulations, you are 8% of the way to writing 'hello, world!'.

Let's dissect our program, line by line. The statement 'DO ,1 <- #1' places the constant value '1' into the first element of an array called ',1'. Constants are prefixed by a 'mesh' (#) and 16-bit arrays are prefixed by a 'tail' (,). (Of course 32-bit arrays are prefixed by a 'hybrid' (;).)

On line two we have a similar command 'DO ,1SUB#1 <- #234' which takes another constant, 234, and places it into the second element of array ,1 using the SUB syntax. (Surprisingly, INTERCAL arrays start at index 0.)

On line three we have the command 'DO READ OUT ,1' which will send the text in our array ,1 to the standard output (which I will assume is a dot-matrix printer in your environment).

On line four we have the command 'PLEASE GIVE UP' which of course ends our program.

Worth noting is the fact that the constant '234' has no obvious correlation to the character output by our program 'h'. I discuss the powerful INTERCAL output algorithm in 'Diving into INTERCAL - Part Dalawa'.

I hope I have sparked your interest in the wonderful world of INTERCAL programming. In future installments we will look at the Turing Text Model and other language features such as FORGET, REMEMBER, ABSTAIN, IGNORE and COME FROM.

(1): Yes, the C-INTERCAL compiler always misspells the word RESUBMIT

(2): You may receive a 'random compiler bug' that will appear at run-time. This seems to happen about 10% of the time.

PS. A massive prize for the first person who leaves a comment explaining why this post was called 'Diving into INTERCAL - Part Isa'

Labels: ,


Blogger Gary Marshall said...

Isa is 1 in Tagalog

March 12, 2007 at 12:52 PM  
Blogger Clinton Forbes said...

Correct, Gary.

Your prize is a 1963 Corvette Sting Ray. Please contact me to pick it up.

March 12, 2007 at 2:29 PM  
Blogger Marcos said...

Ancient thread, but for posterity: the explanation of the first assignment is incorrect. ,1 <- #1 doesn't assign anything to any element of the array. What it does is declare the array to be of length 1. It's the equivalent of a DIM statement in BASIC.

INTERCAL arrays start at SUB #1; an attempt to reference array SUB #0 will yield a runtime error about variables in hyperspace.

May 5, 2011 at 7:07 AM  

Post a Comment

Links to this post:

Create a Link

<< Home