23
$\begingroup$

Here's a simplified version of what I'm trying to do:

SetAttributes[def, HoldFirst]
def[s_Symbol, v_] := (s[x_] := v)
def[f, x^2]
f[3] (* Expected result: 9 *)
(*
  x^2
*)
?f (* Expected result: f[x_] := x^2 *)
(*
  Global`f

  f[x$_] := x^2
*)

Obviously the x in the x_ pattern gets replaced by x$. Is there a way I can prevent that? That is, from calling def[f,x^2] I want to result the definition f[x_] := x^2. I don't of course care about the name of the variable, so if the resulting function definition reads f[x$_] := x$^2 I'm fine with that, too.

I tried

def[s_Symbol, v_] := With[{x$ = x}, s[x_] := v]
def[s_Symbol, v_] := With[{x = x$}, s[x_] := v]
def[s_Symbol, v_] := (s[x_] := v) /. x :> x$

and

def[s_Symbol, v_] := (s[x_] := v) /. x$ :> x

but none worked.

$\endgroup$
3
  • 2
    $\begingroup$ How about SetAttributes[def, HoldFirst]; def[s_Symbol, v_] := With[{temp = v}, s = Function @@ {x, temp}]? $\endgroup$ Aug 31, 2012 at 9:28
  • $\begingroup$ @J.M.: Thanks, I didn't think of anonymous functions; that's a solution that indeed works for my case. $\endgroup$
    – celtschk
    Aug 31, 2012 at 9:36
  • $\begingroup$ @J.M.: I have to retract that it works for my problem: I just noticed that anonymous functions don't seem to support optional arguments. $\endgroup$
    – celtschk
    Aug 31, 2012 at 9:43

3 Answers 3

20
$\begingroup$

With your proposed definition style, the user of that function def has to know that v could/should/must depend on x for this to work; x really should be an argument of def. Perhaps something like this were better suited.

ClearAll[def]
ClearAll[f]
(*SetAttributes[def,HoldFirst]*)

def[s_Symbol, v_, vars_List] := 
 With[{h = s @@ (Pattern[#, Blank[]] & /@ vars)}, (h := v)]
def[f, x^2, {x}]

f[3]
(* 9 *)
$\endgroup$
1
  • $\begingroup$ This indirect pattern building did the trick! $\endgroup$
    – celtschk
    Aug 31, 2012 at 9:51
20
$\begingroup$

Try for example

SetAttributes[def, HoldAll]
def[s_Symbol, v_] := Function[Null, s[x_] := #, HoldFirst][v]

Unnamed functions just don't care :)

Other alternatives that should also work (but I would use the previous approach)

def[s_Symbol, v_] := Identity[SetDelayed][HoldPattern@s[x_], v];
def[s_Symbol, v_] := Unevaluated[s[x_] := "Hello"] /. "Hello" -> v
$\endgroup$
2
  • $\begingroup$ Elegant! +1. (I edited to better respect the semantics of SetDelayed. If you don't like the result, feel free to revert!) $\endgroup$ Aug 31, 2012 at 21:33
  • $\begingroup$ Thanks @OleksandrR. Your edit is fine. I had done it that way based on the question being HoldFirst, but HoldAll makes more sense to me too $\endgroup$
    – Rojo
    Sep 1, 2012 at 1:21
11
$\begingroup$

I propose these:

SetAttributes[def, HoldAllComplete]

def[s_Symbol, v_] := SetDelayed @@ Hold[s[x_], v]

def[s_Symbol, v_] := With[{L := s[x_]}, L := v]

def[s_Symbol, v_] := Reverse @ Unevaluated[v := s[x_]]

The HoldAllComplete (or SequenceHold) attribute is necessary for an assignment such as:

def[q, Sequence[1, 2, x]]

head[q[5]]
head[1, 2, 5]

Also see:

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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