1
$\begingroup$

Mathematically it seems so easy to use two elements of a vector as two arguments to a function. $$f(\boldsymbol{x_{i+1}},\boldsymbol{x_{i}})$$ In functional programming however a map (function) allows to work with one element at a time only, so one would need to create vector of pairwise 2-tuples from a single initial vector fisrt. Why is that? Is it also more proper mathematically?

$\endgroup$
  • $\begingroup$ As you state, there is a method to essentially encode what you want. Why programming languages decide to encode your operation in a certain way is likely a matter of opinion and historical reasons. $\endgroup$ – Discrete lizard Jan 8 '18 at 9:53
  • $\begingroup$ 1) You're missing corner cases. What if $i$ is the last index? The function is not well-defined per se. 2) You can easily "zip" a list with a shifted copy and map that one. $\endgroup$ – Raphael Jan 8 '18 at 12:25
4
$\begingroup$

Conceptually, 1-argument map should be able to work on unordered collections, because each application of a mapping function doesn't know anything about the element's index.

Your proposed version does know something about the elements' indices, so treats its argument as an ordered sequence of elements. That's the difference.

For regular lists, we can recover the indices by zipping the list with the (infinite, unbounded) sequence [1..]. So, the ability to zip assumes the sequence is ordered - not in values, but positionally.

Mathematicians can have some specific terminology for this, but I think that's the gist of it.

Such sequences you can always split into a special so-called head i.e. "first" element, and the rest of them, deterministically. But e.g. for truly non-deterministic sequences this is not possible, yet mapping is still a well-defined operation.

$\endgroup$
  • 2
    $\begingroup$ I like how enumeration is really just additional information (i.e. an ordered set is just a regular unordered set where each element has an additional piece of information). This is somehow elegant, explicit, clear and clean. $\endgroup$ – A.L. Verminburger Jan 8 '18 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.