How would you, given a list {1, 2, 3, 4}
, apply a function f
to 1
and 2
, then 2
and 3
, etc.?
{f[1,2], f[2,3], f[3,4]}
More generally, how do you define which parts of a list you want to pass/Map
/Apply
to a function that takes multiple arguments?
Answer
You might first partition your list and then use Map as usual :
f[#[[1]], #[[2]]] & /@ Partition[{1,2,3,4}, 2, 1]
(* {f[1, 2], f[2, 3], f[3, 4]} *)
Comments
Post a Comment