Live data from Hacker News

Are arrays functions?

futhark-lang.org

51–60 of 145 posts

Re: Are arrays functions?

#51
No, the best thing you can do for simplicity is to not conflate concepts. The perpetual idea of mixing data and execution is a misguided search for a silver bullet and it never makes things better in the long term.

This is cleverness over craftsmanship. Keeping data and execution as separate as possible is what leads to simplicity and modularity.

The exception is data structures which need the data and the functions that deal with it to expose that data conveniently to be closely tied to each other.

Everything else is an unnecessary dependency that obscures what is actually happening and makes two things that could be separated depend on each other.

Re: Are arrays functions?

#52

No, the best thing you can do for simplicity is to not conflate concepts. The perpetual idea of mixing data and execution is a misguided search for a silver bullet and it never makes things better in the long term. This is cleverness over craftsmanship. Keeping data and execution as separate as possible is what leads to simplicity and modularity. The exception is data structures which need the data and the functions…

> No, the best thing you can do for simplicity is to not conflate concepts.

This presumes the framework in which one is working. The type of map is and always will be the same as the type of function. This is a simple fact of type theory, so it is worthwhile to ponder the value of providing a language mechanism to coerce one into another.

> This is cleverness over craftsmanship. Keeping data and execution as separate as possible is what leads to simplicity and modularity.

No, this is research and experimentation. Why are you so negative about someone’s thoughtful blog post about the implications of formal type theory?

Re: Are arrays functions?

#53
When it says "arrays, which may be thought of as functions whose domains are isomorphic to contiguous subsets of the integers", is it saying that this:

    const list = ['a', 'b', 'c']
is syntactic sugar for expressing something like this:

    function list(index) {
      switch (index) {
        case 0: return 'a'
        case 1: return 'b'
        case 2: return 'c'
      }
    }

Re: Are arrays functions?

#54
post #53

When it says "arrays, which may be thought of as functions whose domains are isomorphic to contiguous subsets of the integers", is it saying that this: const list = ['a', 'b', 'c'] is syntactic sugar for expressing something like this: function list(index) { switch (index) { case 0: return 'a' case 1: return 'b' case 2: return 'c' } }

No. Quick version: They have the same type. Both take an integer and return a string, so their type would be Integer -> String in your example.

They are computationally equivalent in the sense that they produce the same result given the same input, but they do not perform the exact same computation under the hood (the array is not syntactic sugar for the function).

For the distinction there, consider the two conventional forms of Fibonacci. Naive recursive (computationally expensive) and linear (computationally cheap). They perform the same computation (given sufficient memory and time), but they do not perform it in the same way. The array doesn't "desugar" into the function you wrote, but they are equivalent in that (setting aside call syntax versus indexing syntax) you could substitute the array for the function, and vice versa, and get the same result in the end.

Re: Are arrays functions?

#55
The definition of a function is that for any given input A, I give you an output B. In fact anything that encodes a kind of transformation and yield new information based an input could be seen as a function. From that point of view, array is a function that when "touched", it gives you the information about its items.

In fact, from wikipedia:

```

In mathematics, a tuple is a finite sequence or ordered list of numbers or, more generally, mathematical objects, which are called the elements of the tuple. An n-tuple is a tuple of n elements, where n is a non-negative integer. There is only one 0-tuple, called the empty tuple. A 1-tuple and a 2-tuple are commonly called a singleton and an ordered pair, respectively. The term "infinite tuple" is occasionally used for "infinite sequences".

Tuples are usually written by listing the elements within parentheses "( )" and separated by commas; for example, (2, 7, 4, 1, 7) denotes a 5-tuple. Other types of brackets are sometimes used, although they may have a different meaning.[a]

An n-tuple can be formally defined as the image of a function that has the set of the n first natural numbers as its domain. Tuples may be also defined from ordered pairs by a recurrence starting from an ordered pair; indeed, an n-tuple can be identified with the ordered pair of its (n − 1) first elements and its nth element

```

(https://en.wikipedia.org/wiki/Tuple)

From a data structure standpoint, a tuple can be seen as an array of fixed arity/size, then if an array is not a function, so shouldn't a tuple too.

Re: Are arrays functions?

#56
post #53

When it says "arrays, which may be thought of as functions whose domains are isomorphic to contiguous subsets of the integers", is it saying that this: const list = ['a', 'b', 'c'] is syntactic sugar for expressing something like this: function list(index) { switch (index) { case 0: return 'a' case 1: return 'b' case 2: return 'c' } }

https://en.wikipedia.org/wiki/Church_encoding

Advanced version (which defines the ADT we use today): https://en.wikipedia.org/wiki/Mogensen%E2%80%93Scott_encodin...

Re: Are arrays functions?

#57
post #3

Everything is a function. Next question?

For the downvoters, I think giving examples of what's NOT a function would start an interesting conversation, especially if you don't know how it could possibly be interesting!

Take a parabola and rotate it 90 degrees. The forumula for that is not a function of the x axis: it has two values for all but one point on the curve.

Re: Are arrays functions?

#59
post #53

When it says "arrays, which may be thought of as functions whose domains are isomorphic to contiguous subsets of the integers", is it saying that this: const list = ['a', 'b', 'c'] is syntactic sugar for expressing something like this: function list(index) { switch (index) { case 0: return 'a' case 1: return 'b' case 2: return 'c' } }

Yes.

Re: Are arrays functions?

#60
post #5

I remember I got a little confused when I was first learning TLA+, because what you normally call "functions" are "operators" [1], and what you'd normally call "maps" or "lists" are called "functions". It was odd to me, because it hadn't really occurred to me before that, given infinite memory (and within a mathematical framework), there's fundamentally not necessarily a difference between a "list" and a "function".…

Reminds me of many years ago when people were fascinated by the discussion about whether closures are objects or objects are closures. Yes... Yes they are.
Post reply on HN