Message Boards Message Boards

0
|
5686 Views
|
1 Reply
|
1 Total Likes
View groups...
Share
Share this post:

Difference between f1[a_, b_] := a[b]^2 and f2[a_][b_] := a[b]^2 etc.

Posted 10 years ago

Colleagues, please - it is a difference betwen theese defintifions? It seems to me, that tey are equivalent -- it is true?

In[1]:= f1[a_, b_] := a[b]*a[2 b]

In[2]:= f2[a_][b_] := a[b]*a[2 b]

In[3]:= f3[a_[b_]] := a[b]*a[2 b]

In[4]:= {f1[Func, x], f2[Func][x], f3[Func[x]]}

Out[4]= {Func[x] Func[2 x], Func[x] Func[2 x], Func[x] Func[2 x]}

Can you give me a link for study this theme? Thanks!

POSTED BY: Tomáš Hruš

The first two accomplish the very same thing, for as long as you keep them in this form.

The third one won't always behave the same way though. Consider

fun[x_] := x^2

f3[a_[b_]] := a[b]*a[2 b]

Then f3[fun[z]] will evaluate to f3[z^2] because fun[z] evaluates first. However, if you set SetAttributes[f3, HoldAll], then it will behave more similarly to f1 and f2:

f3[fun[z]] will now first evaluate to fun[z]*fun[2 z], then to 4 z^4.

This is not the complete story, but giving full details is not always educational. So instead I'll leave you with this reference, which (I think) discusses all the relevant issues:

In particular, look at the section on Nonstandard Evaluation and the Hold... attributes.

POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract