Hacker News new | ask | show | jobs
by thetwiceler 4564 days ago
Perhaps the more interesting question is why do most programming languages allow several input values?

I'll make the argument that in Haskell, functions BOTH support only a single input value and a single output value. For example, consider the function (+) which takes two numbers and returns their sum. The type of (+) is (+) :: Num a => a -> (a -> a). I have added the parentheses to emphasize the syntax of the type. (+) is a function which takes a single number as input and gives a single function as output!

Now, we can do something like f :: (a,b) -> (x,y,z) and write:

  let (x0,y0,z0) = f (a0,b0)
But this isn't really multiple input or return values. We are just inputting or returning a struct (with constructor (,) or (,,), respectively), and we can pattern match against it for the return value.

I think that any elegant language should support as few "primitive features" as possible - it should be simple, and let a few powerful ideas (e.g., first-class functions, currying, and pattern matching) let the rest fall out. I think Haskell really embodies this idea.

1 comments

I've thought about switching to only one input value for Rust, but it's needed for the FFI, so once we're paying the complexity there we might as well pay it everywhere. Besides, it'll make it easier to add optional and named parameters if we ever do in the future.
Wouldn't memory layout on the stack be the same for a tuple vs a sequence of arguments?
Unfortunately calling conventions are hideously complex; some arguments go in registers, some go on the stack, some change depending on whether they're structs or floats or not…it's a giant mess :(