McCarthy 91 function: Difference between revisions

Content deleted Content added
Add category
Code: This isn't rosetta code
 
Line 65:
 
==Code==
Here is an implementation of the nested-recursive algorithm in [[Lisp (programming language)|Lisp]]:
 
<syntaxhighlight lang="lisp">
(defun mc91 (n)
(cond ((<= n 100) (mc91 (mc91 (+ n 11))))
(t (- n 10))))
</syntaxhighlight>
 
Here is an implementation of the nested-recursive algorithm in [[Haskell]]:
 
<syntaxhighlight lang="haskell">
mc91 n
| n > 100 = n - 10
| otherwise = mc91 $ mc91 $ n + 11
</syntaxhighlight>
 
Here is an implementation of the nested-recursive algorithm in [[OCaml]]:
 
<syntaxhighlight lang="ocaml">
let rec mc91 n =
if n > 100 then n - 10
else mc91 (mc91 (n + 11))
</syntaxhighlight>
 
Here is an implementation of the tail-recursive algorithm in [[OCaml]]:
 
<syntaxhighlight lang="ocaml">
let mc91 n =
let rec aux n c =
if c = 0 then n
else if n > 100 then aux (n - 10) (c - 1)
else aux (n + 11) (c + 1)
in
aux n 1
</syntaxhighlight>
 
Here is an implementation of the nested-recursive algorithm in [[Python (programming language)|Python]]:
 
<syntaxhighlight lang="python">
def mc91(n: int) -> int:
"""McCarthy 91 function."""
if n > 100:
return n - 10
Line 112 ⟶ 75:
</syntaxhighlight>
 
Here is an implementation of the nestedtail-recursive algorithm in [[C (programming language)|C]]Python:
 
<syntaxhighlight lang="cpython">
intdef mc91(int n: int) -> int:
return mc91taux(n, 1);
{
if (n > 100) {
return n - 10;
} else {
return mc91(mc91(n + 11));
}
}
</syntaxhighlight>
 
intdef mc91taux(int n,: int, c: int) -> int:
Here is an implementation of the tail-recursive algorithm in [[C (programming language)|C]]:
if c == 0 then n:
 
(t (-return n 10))))
<syntaxhighlight lang="c">
ifelif (n > 100) {:
int mc91(int n)
else if n > 100 then auxreturn mc91taux(n - 10), (c - 1)
{
} else {:
return mc91taux(n, 1);
else aux return mc91taux(n + 11), (c + 1)
}
 
int mc91taux(int n, int c)
{
if (c != 0) {
if (n > 100) {
return mc91taux(n - 10, c - 1);
} else {
return mc91taux(n + 11, c + 1);
}
} else {
return n;
}
}
</syntaxhighlight>