这有标准功能吗?

Max*_*Max 2 wolfram-mathematica

在Mathematica中可能有内置函数或更好更快的方法

func[l_, g_, f_] := g @@ f @@@ Transpose[{Most[l], Rest[l]}]
Run Code Online (Sandbox Code Playgroud)

这可以用来做这样的事情

l = {a, b, c, d}
func[l, Plus, (#1 - #2)^2 &]
Run Code Online (Sandbox Code Playgroud)

我不知道这种功能的正确名称.折叠拉链类型的东西.

更新 Lot的解决方案.谢谢大家.

运用

Partition[l, 2, 1] 
Run Code Online (Sandbox Code Playgroud)

代替

Transpose[{Most[l], Rest[l]}] 
Run Code Online (Sandbox Code Playgroud)

绝对让它更清晰.

我试图在函数上运行时序,但是我得到了奇怪的结果:

func1[l_, g_, f_] := g @@ f @@@ Transpose[{Most[l], Rest[l]}]
func2[l_, g_, f_] := g @@ f @@@ Partition[l, 2, 1]
func3[l_, g_, f_] := g @@ ListConvolve[{1, 1}, l, {-1, 1}, {}, Times, f]
func4[l_, g_, f_] := g @@ Thread[f[Most@l, Rest@l]]
func5[l_, g_, f_] := g @@ f /@ Partition[l, 2, 1]
func6[l_, g_, f_] := g @@ Thread[f[Most[l], Rest[l]]]
func7[l_, f_, g_] := Inner[f, Sequence @@ Partition[l, Length[l] - 1, 1], g]
func8[l_, g_, f_] := g @@ MapThread[f, Partition[l, Length[l] - 1, 1]]
functions = {func1, func2, func3, func4, func5, func6, func7, func8}

input = Table[ToExpression["x" <> ToString[i]], {i, 1, 1000000}];
inputs = Table[Take[input, i*100000], {i, 1, 10}];

Table[
  If[i == j == 0, "",
  If[j == 0, functions[[i]],
  If[i == 0, Length[inputs[[j]]],
    Timing[functions[[i]][inputs[[j]]]][[1]]]]], 
    {i, 0, Length[functions]}, {j, 0, Length[inputs]}] // Transpose // TableForm
Run Code Online (Sandbox Code Playgroud)

Jan*_*nus 6

如果你想要的东西,正是复制您的功能func,我能想到的唯一prettyfication被替换Transpose[Most[l],Rest[l]]Partition:

func2[l_,g_,f_]:=g@@f@@@Partition[l,2,1]
Run Code Online (Sandbox Code Playgroud)

如果你真的想要一些"内置"的东西,那么你可能会对某些东西ListConvolve进行破解

func3[l_,g_,f_]:=g@@ListConvolve[{1,1},l,{-1,1},{},Times,f]
Run Code Online (Sandbox Code Playgroud)

检查所有这些工作:

Through[{func,func2,func3}[l,Plus,(#1-#2)^2&]]
Out[19]= {(a-b)^2+(b-c)^2+(c-d)^2,(a-b)^2+(b-c)^2+(c-d)^2,(a-b)^2+(b-c)^2+(c-d)^2}
Run Code Online (Sandbox Code Playgroud)

最后,如果这是您正在寻找的答案,我建议计算它 Total[Differences[l]^2]

Out[14]= (-a+b)^2+(-b+c)^2+(-c+d)^2 
Run Code Online (Sandbox Code Playgroud)