4 ms·
Can you write a traditional swap(a,b) method/function in the language? What about languages that are immutable by default, like Haskell, OCaml, and Erlang? You
by jganetsk 17y ago
Can you write a traditional swap(a,b) method/function in the language?
What about languages that are immutable by default, like Haskell, OCaml, and Erlang? You can't write a swap function. Therefore, are they pass-by-value?
Actually, in OCaml, you can write the swap function.
let swap a b =
let temp = !a in
a := !b;
b := temp
let x = ref 3 in
let y = ref 8 in
swap x y
But now, you are passing references by value, as Java does. So maybe they are pass-by-value. But, when your language is immutable, pass-by-reference (whatever that would mean), doesn't effect the language semantics. They are pass-by-(it's-moot).
- jerf 17y agoI've really come to believe that all these terms should be explicitly understood to be domain specific. I can not think of a single term in common use that doesn't have edge cases and other such lacunae when it comes to application to real languages. You name the term, "object orientation", "functional", "reference" vs. "pointer", the distinction really only applies to a given language. Even the putative "true" definition is usually only the "true" definition in the context of machine language, and something like a true Lisp machine might still end up with no applicable definition for common terms. (Every time I say this, someone tries to correct me, but it is followed up by someone who then tries to debate the definition being used to correct me. It really is all relative.) So, in this case, the definition being given for "-by-value" vs. "-by-reference" does hold across a wide variety of imperative languages, but as you point out has no meaning in strictly-immutable functional languages since even inside a given function you can't write a "swap", let alone as a function. The terms only have meanings in certain contexts.
- eru 17y agoHaskell is something like pass-by-name. At least staff only gets evaluated at most once. But it's definitely not pass-by-value, since not everything gets evaluated down to a value.
- jganetsk 17y agoYour argument would get ensnared by that of the original post. If you take the description of Java's passing semantics, and s/object/thunk/ and s/primitive/unboxed value/, you get a perfect description of Haskell's passing semantics.
- known 17y agohttp://en.wikipedia.org/wiki/XOR_swap_algorithm http://en.wikipedia.org/wiki/XOR_swap_algorithm
- eru 17y agoIn the StateMonad I can write a swap function in Haskell.
- jganetsk 17y agoNot one that, according to the original poster's definiton, would qualify Haskell as a pass-by-reference language.
- ionfish 17y agoIndeed; pass by reference / pass by value is an irrelevant distinction in a referentially transparent language.