| ▲ | Are arrays functions?(futhark-lang.org) | |||||||||||||||||||||||||||||||||||||||||||
| 44 points by todsacerdoti 2 days ago | 24 comments | ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | tombert an hour ago | parent | next [-] | |||||||||||||||||||||||||||||||||||||||||||
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". With pure functions, you could in "theoretical-land", replace any "function" with an array, where the "argument" is replaced with an index. And it makes sense to me now; TLA+ functions can be defined like maps or lists, but they can also be defined in terms of operations to create the values in the function. For example, you could define the first N factorials like
or like this:
in both cases, if you wanted to get the factorial of 5, you'd call Fact[5], and that's on purpose because ultimately from TLA+'s perspective they are equivalent.[1] At least sort of; they superficially look like functions but they're closer to hygienic macros. | ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | Nevermark an hour ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||
Arrays and structures are functions. And all three are tuple [input, output] pattern matches, with the special case that in “call/select tuples”, input is always fully defined, with output simply being the consequence of its match. And with arrays, structures and overloaded functions being unions of tuples to match to. And structure inputs (I.e. fields) being literal inline enumeration values. And so are generics. In fact, in functional programming, everything is a pattern match if you consider even enumeration values as a set of comparison functions that return the highly used enumerations true or false, given sibling values. | ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | thomasahle 43 minutes ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||
What about replacing > Haskell provides indexable arrays, which may be thought of as functions whose domains are isomorphic to contiguous subsets of the integers. with > Haskell provides indexable arrays, which are functions on the domain [0, ..., k-1]? Or is the domain actually anything "isomorphic to contiguous subsets of the integers"? | ||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | clhodapp 39 minutes ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||
Any expression-value can be a function, if you simply define the meaning of applying the the expression-value to another expression-value to be something compatible with your definition of a function. | ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | bandrami 43 minutes ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||
You can consider any vector a function, though it's not always helpful to do so | ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | stackghost an hour ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||
It makes obvious sense to consider an array as a function with the index as its input argument and the element its output, i.e. f(x) = A[x]... but this isn't the first time I've encountered this and I still don't see the practical benefit of considering things from this perspective. When I'm writing code and need to reach for an array-like data structure, the conceptual correspondence to a function is not even remotely on my radar. I'm considering algorithmic complexity of reads vs writes, managed vs unmanaged collections, allocation, etc. I guess this is one of those things that's of primary interest to language designers? | ||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | SanjayMehta 2 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||
Everything is a function. Next question? | ||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | hahahahhaah an hour ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||
Arrays are objects (allocated memory and metadata if you will). The function is what takes the array and an int and returns an item. | ||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | zaps an hour ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||||||||||||||
idk probably? | ||||||||||||||||||||||||||||||||||||||||||||
| ▲ | shevy-java an hour ago | parent | prev [-] | |||||||||||||||||||||||||||||||||||||||||||
> Haskell provides indexable arrays, which may be thought of as functions whose domains are isomorphic to contiguous subsets of the integers. > I found this to be a hilariously obtuse and unnecessarily formalist description of a common data structure. Well it is haskell. Try to understand what a monad is. Haskell loves complexity. That also taps right into the documentation. > I look at this description and think that it is actually a wonderful definition of the essence of arrays! I much prefer simplicity. Including in documentation. I do not think that description is useful. To me, Arrays are about storing data. But functions can also do that, so I also would not say the description is completely incorrect either. > who can say that it is not actually a far better piece of documentation than some more prosaic description might have been? I can say that. The documentation does not seem to be good, in my opinion. Once you reach this conclusion, it is easy to say too. But this is speculative because ... what is a "more prosaic description"? There can be many ways to make a worse documentation too. But, also, better documentation. > To a language designer, the correspondence between arrays and functions (for it does exist, independent of whether you think it is a useful way to document them) is alluring, for one of the best ways to improve a language is to make it smaller. I agree that there is a correspondence. I disagree that Haskell's documentation is good here. > currying/uncurrying is equivalent to unflattening/flattening an array So, there are some similarities between arrays and functions. I do not think this means that both are equivalent to one another. > would like to see what it would be like for a language to fully exploit the array-function correspondence. Does Haskell succeed in explaining what a Monad is? If not, then it failed there. What if it also fails in other areas with regards to documentation? I think you need to compare Haskell to other languages, C or Python. I don't know if C does a better job with regards to its documentation; or C++. But I think Python does a better job than the other languages. So that is a comparison that should work. | ||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||