Everything2
Near Matches
Ignore Exact
Full Text
Everything2

M4: how the counting loop works

created by ariels

(idea) by ariels (3.8 d) (print)   ?   (I like it!) Sun Jan 28 2001 at 15:01:43

This explains how the M4 ``loop'' in Learn to Count from One to Ten in Different Languages works. For convenience, and since the code is so short (not to mentions the fact that The Powers That Be deciding in their infinite wisdom that languages weren't for computers and got their grubby BIG hands on everything), here it is again:

define(`_count',`$1
ifelse(`$1',`$2',,`_count(incr(`$1'),`$2')')')dnl
define(`count',`_count(1,$1)')dnl
count(10)dnl

First we define the macro _count. It takes 2 arguments, with the first not larger than the second, and counts from the first to the second. M4 macro arguments live in the placeholders $1,$2,... so first we just output $1 and a newline; this ``just happens'' if we write $1 followed by a newline, since all M4 ever does is expand macros.

The next step is to loop if necessary. M4 has a powerful conditional construct named ifelse (see also how to say [else if). Here we use the form ifelse(A,B,C,D), which expands to C if A and B are equal strings, else to D. If $1 and $2 are the same, we've finished counting, so we make the ifelse expand to nothing; M4 finishes processing the input at that point. Otherwise, we expand to another call to _count, with the start value incremented.

The quotes around the entire ``recursive'' call to _count are absolutely necessary! Without them, the call would take place before the ifelse is evaluated, and we'd immediately get an infinite loop.

Finally, count just tells _count to start counting at 1. The dnls are there to suppress spurious newlines.

Clean, elegant, simple. It's a wonder M4 isn't used more often!


printable version
chaos

M4 How to say "else if" infinite loop Anti-Tank Rifle Type 97
Learn how to swear in different languages dnL third shift Oracle
How dating pretty boys helped me come out As seen mouthed through one half inch of safety glass as the bus pulled away from the terminal and left a westward wafting cloud of exhaust M3 Dance Dance Dance
The Counting Game Imprecation How to use an escalator in a wheelchair A Brief History of Time
INTERCAL AM radio
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
After stirring Everything, these nodes rose to the top:
A Study On The Effects Of THC
Gone beyond reasonable
Books made me a person
Summer storm
I will REMOVE the fucking toilet seat if you don't shut up
Walter Matthau
Goldfish
tzitzit
Once you have tasted flight: In defense of manned space travel
Real war: Civilization threatened!
Geta
European Union
condom man page
New Writeups
doctor wilson
Soup, of the green variety(recipe)
Ctrl Y
cognitive dissonance(fiction)
SharQ
Gone Baby Gone(review)
halfWit
If I could, I'd title this "Freedom"(thing)
Roninspoon
Airline Hero(thing)
Ktistec
Why Women Are Always Cold(person)
doctor wilson
Drug policy reform(thing)
tejasa
Easy Raspberry Cheesecake(recipe)
Joysim
Drug policy reform(idea)
aneurin
Tyburn(place)
niruena
Boiling to death(idea)
artman2003
summer(thing)
doctor wilson
The Silver City and the Silent Sea(log)
Dreamvirus
The Silver City and the Silent Sea(poetry)
Aerobe
A nihilist's soulmate(poetry)
E2 is a by-product of the existence of The Everything Development Company