[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Axiom-developer] [MutualRecursion] (new)
From: |
Bill Page |
Subject: |
[Axiom-developer] [MutualRecursion] (new) |
Date: |
Mon, 17 Jan 2005 21:55:14 -0600 |
Here is an example of defining functions by mutual recursion in Axiom
We start with a "bootstrap" definition of 'parity(n)\$Even'. All that is
really needed here is a package that exports a function named 'parity'
with the right signature. This particular function will never be called
and will be re-defined later. It's only purpose is as a placeholder to
allow the later definition of **ODD**.
\begin{axiom}
)abbrev package EVEN Even
Even(): E == I where
E == with
parity: Integer -> Boolean
I == add
parity(n) == true
\end{axiom}
Now we can define 'parity(n)\$Odd'. It depends on **EVEN**.
\begin{axiom}
)abbrev package ODD Odd
Odd(): E == I where
E == with
parity: Integer -> Boolean
I == add
parity(n:Integer) ==
-- output("ODD",n::OutputForm)$OutputPackage
(n>0) => parity(n-1)$Even
(n<0) => parity(n+1)$Even
false
\end{axiom}
But the bootstrap definition of **EVEN** is incomplete. It really
depends (recusively) on **ODD**. So finally we need the full (re-)definition
of 'parity(n)\$Even'
\begin{axiom}
)abbrev package EVEN Even
Even(): E == I where
E == with
parity: Integer -> Boolean
I == add
parity(n) ==
-- output("EVEN",n::OutputForm)$OutputPackage
n>0 => parity(n-1)$Odd
n<0 => parity(n+1)$Odd
true
\end{axiom}
Now we can test the new functions:
\begin{axiom}
parity(10)$Even
parity(8)$Odd
parity(-1111)$Odd
\end{axiom}
--
forwarded from http://page.axiom-developer.org/zope/mathaction/address@hidden
- [Axiom-developer] [MutualRecursion] (new),
Bill Page <=
RE: [Axiom-developer] [MutualRecursion] (new), Page, Bill, 2005/01/18