Mutual recursion: Difference between revisions
Content added Content deleted
m (→{{header|Déjà Vu}}: Updates example) |
(→{{header|TXR}}: Replace obsolete approach with modern TXR.) |
||
Line 2,096: | Line 2,096: | ||
=={{header|TXR}}== |
=={{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>@( |
<lang txr>@(do |
||
(defun f (n) |
|||
(if (>= 0 n) |
|||
1 |
|||
(- n (m (f (- n 1)))))) |
|||
@ (or) |
|||
@ (next `!echo $(( @n - 1 ))`) |
|||
⚫ | |||
@ (f n1 fn1) |
|||
@ (m fn1 mfn1) |
|||
@ (next `!echo $(( @n - @mfn1 ))`) |
|||
@ out |
|||
@ (end) |
|||
@(end) |
|||
@(define m (n out)) |
|||
@ (local n1 mn1 fmn1) |
|||
@ (cases) |
|||
⚫ | |||
@ (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) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
(- 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> |
|||
⚫ | |||
f(0) = 1; m(0) = 0 |
f(0) = 1; m(0) = 0 |
||
f(1) = 1; m(1) = 0 |
f(1) = 1; m(1) = 0 |