Suppose I have a function foo which I want to, conceptually at least,
return multiple values: val1, val2, say. Is there a way of assigning
multiple variables to these values in a nice compact way? ... something
like:
(set-something a b (foo arg1 arg2 arg3 ...))
so that a would be set to val1, and b would be set to val2.
Surely it's the sort of pattern that comes up all the time??
Mark Carter <··@privacy.net> writes:
> Suppose I have a function foo which I want to, conceptually at least,
> return multiple values: val1, val2, say. Is there a way of assigning
> multiple variables to these values in a nice compact way?
> ... something like:
> (set-something a b (foo arg1 arg2 arg3 ...))
> so that a would be set to val1, and b would be set to val2.
>
> Surely it's the sort of pattern that comes up all the time??
(setf (values a b) (foo ...))
-Peter
--
Peter Seibel * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp * http://www.gigamonkeys.com/book/
Peter Seibel <·····@gigamonkeys.com> writes:
> Mark Carter <··@privacy.net> writes:
>
>> Suppose I have a function foo which I want to, conceptually at least,
>> return multiple values: val1, val2, say. Is there a way of assigning
>> multiple variables to these values in a nice compact way?
>> ... something like:
>> (set-something a b (foo arg1 arg2 arg3 ...))
>> so that a would be set to val1, and b would be set to val2.
>>
>> Surely it's the sort of pattern that comes up all the time??
>
> (setf (values a b) (foo ...))
>
No, one suggested using 'multiple-value-setq', is there any reason why it
wasn't suggested?
Thanks.
--
Surendra Singhi
http://www.public.asu.edu/~sksinghi/index.html
,----
| "War is Peace! Freedom is Slavery! Ignorance is Strength!"
| -- Orwell, 1984, 1948
`----
Surendra Singhi <·········@netscape.net> writes:
> Peter Seibel <·····@gigamonkeys.com> writes:
>
>> Mark Carter <··@privacy.net> writes:
>>
>>> Suppose I have a function foo which I want to, conceptually at least,
>>> return multiple values: val1, val2, say. Is there a way of assigning
>>> multiple variables to these values in a nice compact way?
>>> ... something like:
>>> (set-something a b (foo arg1 arg2 arg3 ...))
>>> so that a would be set to val1, and b would be set to val2.
>>>
>>> Surely it's the sort of pattern that comes up all the time??
>>
>> (setf (values a b) (foo ...))
>>
>
> No, one suggested using 'multiple-value-setq', is there any reason why it
> wasn't suggested?
In my case, for the same reason I use:
(setf (car x) 10)
instead of:
(rplaca x 10)
It seems to me more stylish to use SETF for all assignments and let it
worry about the underlying primitives needed since a SETF form shows
you what place is being modified whereas primitives such as RPLACA
don't.
-Peter
--
Peter Seibel * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp * http://www.gigamonkeys.com/book/
On Wed, 09 Nov 2005 15:44:30 -0700, Surendra Singhi <·········@netscape.net> wrote:
> No, one suggested using 'multiple-value-setq', is there any reason
> why it wasn't suggested?
It was suggested.
--
Lisp is not dead, it just smells funny.
Real email: (replace (subseq ·········@agharta.de" 5) "edi")
On Wed, 09 Nov 2005 23:44:30 +0100, Surendra Singhi
<·········@netscape.net> wrote:
>
> No, one suggested using 'multiple-value-setq', is there any reason why it
> wasn't suggested?
>
> Thanks.
>
Fine if you prefer
(multiple-value-setq (x y z) (foo))
to
(setf (values x y z) (foo))
be my guest.
Under the hood it get's converted to it anyhow.
I'd say..
1. Because the setf forms provide a more consistent interface.
2. It is much terser
3. it is easier to read
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Mark Carter <··@privacy.net> writes:
> Suppose I have a function foo which I want to, conceptually at least,
> return multiple values: val1, val2, say. Is there a way of assigning
> multiple variables to these values in a nice compact way? ... something
> like:
> (set-something a b (foo arg1 arg2 arg3 ...))
> so that a would be set to val1, and b would be set to val2.
(setf (values a b) (foo arg1 arg2 arg3 ...))
> Surely it's the sort of pattern that comes up all the time??
Not really.
Christophe
Christophe Rhodes wrote:
>>Surely it's the sort of pattern that comes up all the time??
>
> Not really.
To the OP: In (mostly) functional languages one should prefer binding to
mutating. I.e.:
(multiple-value-bind a b c (foo ...)
...use a b c...)
or
(ulet ((a b c (foo ...)))
...)
where ULET ("Unified LET") is left as an exercise ...
Thomas Schilling <······@yahoo.de> wrote:
+---------------
| Christophe Rhodes wrote:
| >>Surely it's the sort of pattern that comes up all the time??
| > Not really.
|
| To the OP: In (mostly) functional languages one should prefer binding to
| mutating. I.e.:
|
| (multiple-value-bind a b c (foo ...)
| ...use a b c...)
+---------------
In Common Lisp, it's written this way:
(defun foo ()
(values 23 45 67))
(multiple-value-bind (a b c)
(foo) ; This is usually indented 4 spaces
...use a b c...)
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
+ Thomas Schilling <······@yahoo.de>:
| To the OP: In (mostly) functional languages one should prefer binding to
| mutating. I.e.:
|
| (multiple-value-bind a b c (foo ...)
| ...use a b c...)
Should be:
(multiple-value-bind (a b c) (foo ...)
...use a b c...)
Whether to make new bindings or to mutate existing ones depends of
course on circumstance, but one certainly needs to be aware of
multiple-value-bind. In practice, it is probably used more often.
--
* Harald Hanche-Olsen <URL:http://www.math.ntnu.no/~hanche/>
- Debating gives most of us much more psychological satisfaction
than thinking does: but it deprives us of whatever chance there is
of getting closer to the truth. -- C.P. Snow
On Wed, 09 Nov 2005 20:34:32 +0000, Mark Carter <··@privacy.net> wrote:
> Suppose I have a function foo which I want to, conceptually at least,
> return multiple values: val1, val2, say. Is there a way of assigning
> multiple variables to these values in a nice compact way? ...
> something like:
> (set-something a b (foo arg1 arg2 arg3 ...))
> so that a would be set to val1, and b would be set to val2.
>
> Surely it's the sort of pattern that comes up all the time??
<http://www.lispworks.com/documentation/HyperSpec/Body/m_mult_2.htm>
Or like so:
CL-USER 1 > (defun foo ()
(values 42 43 44))
FOO
CL-USER 2 > (defun bar ()
(let (a b c)
(setf (values a b c) (foo))
(list a b c)))
BAR
CL-USER 3 > (bar)
(42 43 44)
Cheers,
Edi.
--
Lisp is not dead, it just smells funny.
Real email: (replace (subseq ·········@agharta.de" 5) "edi")
Edi Weitz wrote:
> On Wed, 09 Nov 2005 20:34:32 +0000, Mark Carter <··@privacy.net> wrote:
>
>
>>Suppose I have a function foo which I want to, conceptually at least,
>>return multiple values: val1, val2, say. Is there a way of assigning
>>multiple variables to these values in a nice compact way? ...
>>something like:
>>(set-something a b (foo arg1 arg2 arg3 ...))
>>so that a would be set to val1, and b would be set to val2.
> <http://www.lispworks.com/documentation/HyperSpec/Body/m_mult_2.htm>
Many thanks to Edi, Peter and Christophe for your responses.
I'm thinking that, as a n00b, knowing so few of the functions, I tend to
write Lisp "the hard way" - little realising that the Lisp language
designers had already anticipated common needs, and provided it" (or
else realising that they probably provided it, but just trying to work
out what it is)
I recently took the step of buying Corman Lisp - requiring quite a lot
of humming and harring on my part. It can be quite hard to decide to
fork out for such things out of one's own pocket - especially realising
that there are free languages out there that are quite good; and that
it's very unlikely to help my employment prospects.
Mark Carter wrote:
> I'm thinking that, as a n00b, knowing so few of the functions, I tend to
> write Lisp "the hard way" - little realising that the Lisp language
> designers had already anticipated common needs, and provided it" (or
> else realising that they probably provided it, but just trying to work
> out what it is)
Reading others' Lisp is a good way of discovering new functions. Also,
CLHS's Permuted Symbol Index makes for good browsing.
> I recently took the step of buying Corman Lisp - requiring quite a lot
> of humming and harring on my part. It can be quite hard to decide to
> fork out for such things out of one's own pocket - especially realising
> that there are free languages out there that are quite good; and that
> it's very unlikely to help my employment prospects.
I won't argue that large numbers of employers are going to see Corman on
your resume and beat a path to your door. But anything you do to raise
yourself above the mainstream language drones is good, right? And if you
end up doing stuff with Lisp that you couldn't do with other languages,
that will look great, too.
Cameron MacKinnon wrote:
> I won't argue that large numbers of employers are going to see Corman on
> your resume and beat a path to your door. But anything you do to raise
> yourself above the mainstream language drones is good, right?
And on the subject of employers ...
I did a PhD 1993-97, which was in mathematics, but had a lot of FORTRAN
progamming in it. The best decision I ever made was to learn C whilst I
was doing my PhD, because C was something employers were really into;
whereas FORTRAN was, well, less so, shall we say.
What I also noticed talking to job agencies and employers at the time,
that somehow programming in academia is different to programming in
industry. Employers seem to be under the illusion that two independent
parts of the brain are required for the different types; the front lobe
part being for academic programming, and the back lobe being for
business programming, or something.
I also sense that contributing to open-source projects doesn't cut much
ice with employers. They don't seem to like a communist-style
programming compared to capitalist-style programming.
Sometimes you might see a more enlightened employer. Whilst I was
already employed by my current company, I was contacted by a potential
client who was interested in using me for my python experience, which he
saw on my website. His comment was that he didn't see many people being
able to offer such experience, and that it was good to see programmers
actually take an interest in programming (or words to that effect).
Bonus points were had for having one's own website.
In the end, nothing came of it; but it's interesting to note that some
employers are able to open their minds just a little. I heard that there
was one company (can't remember which one - sounds the sort of thing
Google would do - but I doubt it's actually them) who didn't employ
programmers unless they had they own website.
In my experience, I find the stuff I do for my employer do be largely
unrewarding compared to the stuff I do in my free time. Mind you, I
mostly have to do Excel spreadsheets with some VBA tucked inside, so
'nuff said, innit.