Jump to content

Mutual recursion: Difference between revisions

→‎{{header|TXR}}: Replace obsolete approach with modern TXR.
m (→‎{{header|Déjà Vu}}: Updates example)
(→‎{{header|TXR}}: Replace obsolete approach with modern TXR.)
Line 2,096:
=={{header|TXR}}==
 
=={{trans|Racket}}==
This is a pain in a language which doesn't yet expose the numeric type that is available internally! So we shell out to get expression evaluation.
 
<lang txr>@(define f (n out))do
@ (localdefun n1f fn1 mfn1(n)
@ (casesif (>= 0 n)
@ (bind n "0") 1
@ (- n (m (f (bind- outn "1"))))))
@ (or)
@ (next `!echo $(( @n - 1 ))`)
@ n1
@ (f n1 fn1)
@ (m fn1 mfn1)
@ (next `!echo $(( @n - @mfn1 ))`)
@ out
@ (end)
@(end)
@(define m (n out))
@ (local n1 mn1 fmn1)
@ (cases)
@ (bind n "0")
@ (bind out "0")
@ (or)
@ (next `!echo $(( @n - 1 ))`)
@ n1
@ (m n1 mn1)
@ (f mn1 fmn1)
@ (next `!echo $(( @n - @fmn1 ))`)
@ out
@ (end)
@(end)
@(next `!seq 0 15`)
@(collect :vars ())
@ n
@ (f n fn)
@ (m n mn)
@ (output)
f(@n) = @fn; m(@n) = @mn
@ (end)
@(end)</lang>
 
(defun m (n)
<pre>$ txr hofs-male-female.txr
@ (bindif n(>= "0" n)
@ n1 0
(- n (f (m (- n 1))))))
 
(each ((n (range 0 15)))
(format t "f(~s) = ~s; m(~s) = ~s\n" n (f n) n (m n))))</lang>
 
<pre>$ txr hofsmutual-male-femalerecursion.txr
f(0) = 1; m(0) = 0
f(1) = 1; m(1) = 0
543

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.