m4-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

recursive countDown macro


From: daniel daniel
Subject: recursive countDown macro
Date: Wed, 10 Aug 2011 14:19:39 -0700

Hi guys,
I felt like writing a recursive countdown macro.

CODE:
define(countDown,`$1
`'ifelse($1,0,,`$0'(`eval'($1 - 1)))')dnl
countDown(10)

OUTPUT:
10
9
8
7
6
5
4
3
2
1
0

DISCUSSION:
In m4, recursion is more readable with ifelse,
where the first argument of ifelse is the
terminating condition. So, in my countDown
macro, the fourth argument of ifelse is a
call to $0 with a nested eval() to subtract
one from the argument and pass the result
as the argument recursively. (The argument
is also printed with a newline at each
iteration).

reply via email to

[Prev in Thread] Current Thread [Next in Thread]