Re: How to update an agrument passed by name in scheme
- From: Anton van Straaten <anton@xxxxxxxxxxxxxxxx>
- Date: Thu, 02 Feb 2006 08:20:12 GMT
H. wrote:
I have to admit I don't get this part of Scheme, in that a lot of people say "say how much easier it is to do this thing in Scheme as opposed to whatever other language". But passing by reference is exceptionally easy in some of those other languages, and not so easy in Scheme, which, based on this thread, cannot be done trivially.
Simplifying a bit, non-atomic values (pairs, vectors etc.) *are* passed by reference in Scheme. The OP talked about "passed by name", which is a little different.
E.g. given the expression (let ((a 5)) (foo a) a), if we happen to know that foo is an ordinary procedure, then without even knowing what foo does, we can safely conclude that the result of the expression is 5. There's no way for foo to affect the value of the variable a, because the variable is passed to foo "by value". This is a good thing for all sorts of reasons, and is pretty fundamental for functional programming.
But given the expression (let ((a (cons 1 2))) (foo a) a), we have no idea what the result will be, without knowing what foo does. Although just as in the previous paragraph, foo cannot affect "the value of the variable a", the value of the variable a is a *reference* to a pair, and it's that reference which is passed to foo. If foo mutates the referenced pair, e.g. using set-cdr!, the result of the above expression could end up being, say, the improper list (1 9 8 . 7).
So, it's hard to argue that passing by reference isn't exceptionally easy in Scheme. (Or at least, you need to argue it better ;)
If what you really mean is passing by name, then it's easy to use a macro to do that. Exceptionally easy, once you know how.
Usually, I can code things quicker in Scheme, and use it to prototype recursions. But when my homework includes pass-by-reference examples, using functional programming as a modeling tool breaks down.
In _pure_ functional programming, objects don't have any identity independent of their value, so pass-by-reference is a redundant concept.
References to values become an issue when two apparently identical values are actually different in some (usually) invisible way.
For example, in a pure functional language, the pairs constructed by the two expressions (cons 1 2) and, say, (cons 1 (+ 1 1)), are identical in every respect, and there's no way to distinguish between them within the language. The bazillion different kinds of equality that were being discussed the other day are a non-issue in this context.
Once you introduce mutation, though, all that goes out the window. Two apparently identical pairs are actually different -- they essentially have a hidden identity which can be tested using EQ?, and they can be modified independently of each other, with operators like set-car!, further proving that they're not both the same object.
If you want to "use functional programming as a modeling tool" in these cases, you have to actually recognize what's going on, and explicitly model what's really happening, such as the fact that you're really dealing with a stream of values which change over time. (SICP has a good intro to this.)
Scheme, of course, is not a pure functional language, so there are many more options. You'd have to give an example of how Scheme is making it difficult for you to deal with pass by reference, or pass by name for that matter. Perhaps it's because you're expected to use a subset of the language?
(I also don't get why vectors cannot be multidimensional, since, again, using nested i j loops in other languages is an easy thing to do, but I got lost in the conversation on nested vectors in Scheme, all of the ways that by writing a lot of code you could begin to emulate the nested functionality, on Tuesdays, and if you're lucky. I'm being sarcastic, obviously: my real query is why things like pass-by-reference and multi-dimensional vectors can't be more easily done, and why they aren't viewed as core language constructs.)
Pass-by-reference is a core language construct. Pass-by-name is provided via syntactic abstraction, which reflects its nature.
Multi-dimensional arrays don't need to be core language constructs, because a multi-dimensional array is just a special case of one or more single dimensional arrays. It would make sense for them to be a standard library feature, and that's why there are SRFIs which implement them.
It's not nearly as difficult to implement a multi-dimensional array as all that, btw. A couple of dozen lines of code, and you're all set.
Anton .
- Follow-Ups:
- Re: How to update an agrument passed by name in scheme
- From: Matthias Blume
- Re: How to update an agrument passed by name in scheme
- From: Pascal Bourguignon
- Re: How to update an agrument passed by name in scheme
- References:
- How to update an agrument passed by name in scheme
- From: artejera
- Re: How to update an agrument passed by name in scheme
- From: Anton van Straaten
- Re: How to update an agrument passed by name in scheme
- From: Joe Marshall
- Re: How to update an agrument passed by name in scheme
- From: H.
- How to update an agrument passed by name in scheme
- Prev by Date: Re: normal order vs. applicative order question
- Next by Date: Re: Stair-Stepping in style
- Previous by thread: Re: How to update an agrument passed by name in scheme
- Next by thread: Re: How to update an agrument passed by name in scheme
- Index(es):
Relevant Pages
|