Quantcast
Channel: PTC Community : Discussion List - PTC Mathcad
Viewing all articles
Browse latest Browse all 2936

Feature Request: Mutual Recursion

$
0
0

Several classes of problem (eg, context-free grammar interpreters) are easily thought about and implemented by mutually recursive function calls,

 

For example, one of the Rosetta Code programming challenges is to implement the Hofstadter Female and Male Sequences (http://rosettacode.org/wiki/Mutual_recursion#PureBasic).  As far as I'm aware, this isn't possible in Mathcad, but appears to be in a whole host of other languages including Matlab and Mathematica (two of Mathcad's major competitors).   As the ultimate humilation, I wrote my first program in LibreOffice Basic to show that even a word processor can do mutual recursion whereas a mathematical application can't.  Indeed, it seems as if even a pocket calculator can do it (see Ti-89 entry in the Rosetta listing).

 

Stuart

 

Rosetta Code Hofstadter Female and Male Sequence Challenge


Two functions are said to be mutually recursive if the first calls the second, and in turn the second calls the first.


Write two mutually recursive functions that compute members of the Hofstadter Female and Male sequences defined as:


F(0) = 1 ; M(0)=0

F(n) = n - M(F(n-1)), n > 0

M(n) = n - F(M(n-1)), n > 0

 

n    = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

F(n) = 1 1 2 2 3 3 4 5 5 6  6  7  8  8  9  9 10 11 11 12 13

M(n) = 0 0 1 2 2 3 4 4 5 6  6  7  7  8  9  9 10 11 11 12 12


LibreOffice Basic Function Definitions:

 

Function F(n as long) as long

    If n = 0 Then

        F =  1

    ElseIf n > 0 Then

        F = n - M(F(n - 1))

    EndIf

End Function

 

Function M(n)

    If n = 0 Then

        M = 0

    ElseIf n > 0 Then

        M = n - F(M(n - 1))

    EndIf

End Function


Viewing all articles
Browse latest Browse all 2936

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>