开发者

Correct User Defined Version of Map Function in Mathematica?

开发者 https://www.devze.com 2023-01-25 06:43 出处:网络
I\'m trying to create a user defined version of the Map[] function in Mathematica and I\'m running into a few problems.

I'm trying to create a user defined version of the Map[] function in Mathematica and I'm running into a few problems.

Here is what I have so far:

map[x_, s_List] := mapAux[x, s, {}];
mapAux[x, s, {}] :=  Append[{}, First[s]];
mapAux[x, Rest[s], {}];

I'm trying to use it as

map[# + 1 &, {3, 6, 8}]

but this gives a mysterious error beside the output:

 Rest::normal: Nonatomic expression expected at position 1 in Rest[s].

 mapAux[#1 + 开发者_如何学编程1 &, {3, 6, 8}, {}]

The ideal result would be {4,7,9}. I researched the "Nonatomic expression" error and I'm not sure what it means. I'm passing a list to it, but it's just exploding!


You're not passing s or x as variables, so it's just seeing s (which is an atomic expression) rather than a list. You're definition needs to be mapAux[x_, s_, {}]:=..., which will make x and s take the values of the passed parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消