4 ms·
Multiple values are not "just" an optimization hack, they are designed to be convenient to use. I don't know which construct you find "weird". Multiple-value-bi
by junke 11y ago
Multiple values are not "just" an optimization hack, they are designed to be convenient to use. I don't know which construct you find "weird". Multiple-value-bind is a descriptive name which is long to type but this does not matter (type m-v-b + auto-complete).
Typical example: you read a line in a file. The line you get is terminated either by a newline character or by the end-of file. The implementation of read-line[1] must know if you reached end of file or not, but most of the time, you don't care about it. Sometimes, you want to know, and then you can take the secondary value (missing-newline-p).
I like the fact that I can focus on the primary value without caring about other ones when I want.
[1] http://clhs.lisp.se/Body/f_rd_lin.htm http://clhs.lisp.se/Body/f_rd_lin.htm
- JoeAltmaier 11y agoSounds like methods could also add special-purpose multiple values later, without impacting existing callers. Which is nice too.
- qwertyuiop924 11y agoMaybe this is different in CL, but in scheme, if you're using a procedure that returns multiple values, there is no default return, or wrapping the multiple values into a list: You have to use call-with-values, any number of srfis providing sugar, most commonly srfi-8, although 11 and 71 also exist, and without the sugar, god knows how many lambdas. EVERY. SINGLE. TIME. You want to call a function that returns multiple values.
- lispm 11y agoYou might want to learn Common Lisp BEFORE you make statements about how a feature of the language is obsolete or not.
- qwertyuiop924 11y agoI was talking about the general idea of MVR, AND how I thought is was implemented in lisp. I was wrong. This happens sometimes. And even the new explanation, I STILL don't like MVR. I think that returning a list or other data structure, or just writing two functions, is much clearer, and lends itself better to function chaining. Sure, in CL there's a default, which makes things a little better, but what if you want the other one? I don't want to write (multiple-value-bind (a b) (foo bar baz) b) all over my codebase, and the scheme equivalent is just as bad, if not worse.
- junke 11y agoYou don't have to write it all over the codebase. If you have a specific need, defmacro yourself out of your problem. Generally speaking, taking a secondary value alone feels like a bad use of MVR (it happens rarely with standard functions and most libraries). MVR are not intended to be substitute to other data-structures. As said elsewhere, you can and should use structs, classes, lists, hash-tables,... After all, why do we use multiple values instead of a single struct for functions arguments? Because in CL and other languages, unlike in Haskell, you don't want to define an ADT for all your functions or rely on currying. But if you start putting too many coupled parameters in your functions, it starts to smell fishy. Extracting a single value is easy, by the way: (nth-value 1 (foo bar baz))
- lispm 11y agoWhy don't you take a bit of time to read about multiple values in Common Lisp? CL-USER 76 > (nth-value 1 (values 'a 'b 'c 'd)) B While you are at it, check out the feature of 'Macros' in Lisp, which allows everyone to write code to shorten this stuff. See my code in this thread for a macro which then allows you to write: (multiple-value-bind-some (NIL b) (foo bar baz) b) One then can name ignored variables as NIL in the source code...
- qwertyuiop924 11y agoGood point. And very true. Still looks a bit ugly to me, but it's better than nothing, I suppose. And yes, I am aware of macros. For some reason using them in this situation didn't occur to me, so I guess I am an idiot after all. This is actually one of the cases where syntax-rules would allow you to write something cleaner, I would guess. I like syntax rules, but everybody always seems to complain about it...