Live data from Hacker News

Are arrays functions?

futhark-lang.org

41–50 of 145 posts

Re: Are arrays functions?

#41

No - An array is a data structure that stores pre-calculated values in memory, whereas a function is executable logic that computes a result only when it is called.

Correct. But indexing into an array is logic that computes a result when it is called.

Re: Are arrays functions?

#44

No - An array is a data structure that stores pre-calculated values in memory, whereas a function is executable logic that computes a result only when it is called.

Not a semantic difference, just a performance difference ... and a function can cache for the same performance anyway.

Re: Are arrays functions?

#45

No - An array is a data structure that stores pre-calculated values in memory, whereas a function is executable logic that computes a result only when it is called.

Correct. But indexing into an array is logic that computes a result when it is called.

There are two opposing philosophical viewpoints here. Some view mathematics as a model for understanding the real world. Some see the real world as instantiation of mathematics.

Is an array a function? From one perspective, the array satisfies the abstract requirements we use to define the word "function." From the other perspective, arrays (contiguous memory) exist and are real things, and functions (programs) exist and are something else.

Re: Are arrays functions?

#46
The analog of a dot product of vectors is an integral over the product of functions.

The matrix multiplication of vectors - or a row and a column vector - which is then just taking the dot product is called an inner product. So for functions the inner product is an integral over where the functions are defined -

= \int f(x) g(x) dx

Likewise you can multiply functions by a "kernel" which is a bit like multiplying a vector by a matrix

= \int \int A(x,y) f(y) g(x) dx dy

The fourier transform is a particular kernel

Re: Are arrays functions?

#47
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".…

I remember having a similar sort of realization early in my career when trying to implement some horribly convoluted business logic in SQL (I no longer remember the actual details of what I was trying to do, just the epiphany which resulted; I think it had something to do with proration of insurance premiums and commissions): I realized that if I simply pre-computed the value of the function in question and shoved it into a table (requiring "only" a couple million rows or so), then I could use a join in place of function application, and be done with it. An obvious idea in retrospect, but the sudden dredging of the set-theoretic formulation of functions--that they are simply collections of tuples--from deep within my memory was certainly startling at the time.

Re: Are arrays functions?

#48

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"?

In Haskell specifically, arrays really do allow for the more general definition. This makes the library documentation[1] quite a bit more intimidating to newcomers (speaking from personal experience), but saves you the boilerplate and hassle of figuring out the mapping yourself if you're indexing your array by some weird nonsense like `[(False, 'a', 5000, 0)..(True, 'z', 9001, 4)] :: (Bool, Char, Integer, Int8)`.

[1] https://hackage.haskell.org/package/array-0.5.8.0/docs/Data-...

Re: Are arrays functions?

#50
Can you? Yes, and TFA demonstrates this quite clearly.

Should you?

This is where I'd be more careful. Maybe it makes sense to some of the langs in TFA. But it reminds me of [named]tuples in Python, which are iterable, but when used as tuples, in particular, as heterogeneous arrays¹ to support returning multiple values or a quick and dirty product type (/struct), the ability to iterate is just a problem. Doing so is almost always a bug, because iteration through a such tuple is nigh always nonsensical.

So, can an array also be f(index) -> T? Sure. But does that make sense in enough context, or does it promote more bugs and less clear code is what I'd be thinking hard about before I implemented such a thing.

¹sometimes tuples are used as an immutable homogeneous array, and that case is different; iteration is clearly sane, then

Post reply on HN