DEV Community

Discussion on: Elm functions in WebAssembly

Collapse
 
rwoodnz profile image
Richard Wood

Isn't one point of lambda functions that they can be curried functions with one argument each? So can you simplify the underlying function data structure to that?

Collapse
 
briancarroll profile image
Brian Carroll

Hi Richard thanks for reading. Well I suppose there are different dimensions to "simplicity". The idea about functions having one argument is certainly simple in a mathematical sense. But focusing on implementation, I'm not sure it simplifies any of the important things.

I think you're suggesting a structure more like a linked list, with one argument per cell?

Linked lists are great in some situations but the downside is that they end up spreading their data all over the heap. You have to follow the pointers, loading data from a different address each time. On modern CPUs, this access pattern is extremely slow. One memory access takes about 300 times longer than a typical instruction. I'd much rather use a nice tightly packed structure that can be loaded into cache all in one go.

I suppose the simplicity this idea buys you is getting rid of the integer field for the arity, and making the structure fixed size instead of variable size. But I don't think those things would really get rid of much code. It might actually add code.

Also note that there is only one piece of code that looks at the internals of this structure - the "apply" operator. That handles every function call in every Elm program, so it really needs to be fast. You can see it here if you like.