Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm searching for a way to define the function this wikipedia article call Primitive Recursion. It should have two functions as argument and it should return a function. I need this as helping in studying mathematical recursion theory. I need another help with a similar problem; this time i want a "primitive minimization", that is a function called primMin a try i made inspired by @Rojo is.

primMin[f_] := Module[{h},
    h[x___Integer] := Module[{}, For[i = 0, f[x, i] != 0, i++,]; i];

this take f a two argument function and return a one argument function that given x as an argument and search for the first second argument so that f[x,#] == 0. this code work but i couldn't find a way to substitute the For with a Map or something similar. I need that given x there is no y so that f[x,y] == 0 then program will crash or continue to run forever. thank

share|improve this question
Hello! What have you tried, where are you getting stuck, and etc etc? (Typical questions so we feel enablers of your MMA learning and not unpaid employees ) – Rojo Jan 21 '14 at 2:04
up vote 3 down vote accepted

This is a sketch of a solution that only required on the fly translation from the article, done by someone who has no idea what he is doing. See if it helps (and if it works)

primRec[f_, g_] := Module[{h},
  h[0, x___Integer] := f[x];
  h[ym1_Integer, x___Integer] := With[{y = ym1 - 1},
    g[y, h[y, x], x ]

I think this would be a pure function version.

prRec[f_, g_] :=If[#1 === 0, f[##2], g[#1 - 1, #0[#1 - 1, ##2], ##2]] &
share|improve this answer
This is exatcly what i needed my problem were i hadn't familiarity with function like With or Module, I will be testing this and eventually report any misbehavior if any – KiriaKasis Jan 21 '14 at 13:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.