Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am a new Mathematica user, learning the intricacies of functional programming. I have issues withe applying a function over a moving list. My function seems to work and give correct results but it also shows a syntax error. Can you advise how I can get rid of the syntax errors?

movingMap[f_, mylist_, args_, r_] := 
  ListConvolve[ConstantArray[1, r], mylist, {-1, 1}, {}, Times, f &];

yalist = {a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10};

testfunction[x_, y_, z_, w_] := w/6*(x + 4 y + z);

movingMap[testfunction[##, 1], yalist, t, 3]

This gives me the correct results but also gives a syntax warning.

{1/6 (a0 + 4 a1 + a2), 1/6 (a1 + 4 a2 + a3), 1/6 (a2 + 4 a3 + a4), 1/6 (a3 + 4 a4 + a5), 1/6 (a4 + 4 a5 + a6), 1/6 (a5 + 4 a6 + a7), 1/6 (a6 + 4 a7 + a8), 1/6 (a7 + 4 a8 + a9), 1/6 (a10 + a8 + 4 a9)}

I would appreciate advice on getting rid of the syntax error.

share|improve this question
2  
Move the & from within movingMap to testfunction[##,1]&. The simple reason: you can pass in functions using their heads alone that way, e.g. define tf[x__] := testfunction[x] and run movingMap[tf, yalist,t,3]. –  rcollyer Mar 12 '13 at 20:27
    
Wow that was fast… –  Pam Mar 12 '13 at 20:31
1  
I guess I still need to get my Head about things :-) –  Pam Mar 12 '13 at 20:31
    
No worries. And, puns are appreciated. :) –  rcollyer Mar 12 '13 at 20:33
    
@rcollyer I see an empty answer area... –  Sjoerd C. de Vries Mar 12 '13 at 21:33

1 Answer 1

up vote 4 down vote accepted

As my comment answers the question, move the & from within movingMap to testfunction[##,1]&, as follows:

movingMap[f_, mylist_, args_, r_] := 
  ListConvolve[ConstantArray[1, r], mylist, {-1, 1}, {}, Times, f];

movingMap[testfunction[##, 1]&, yalist, t, 3]

The simple reason: you can pass in functions using their heads alone that way, e.g. define

tf[x__] := testfunction[x] 

and run

movingMap[tf, yalist, t, 3]
share|improve this answer

Your Answer

 
discard

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.