109 lines
7 KiB
HTML
109 lines
7 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<title>CLiki: Named multiple-values</title>
|
||
|
<link rel="alternate" type="application/atom+xml" title="ATOM feed of edits to current article"
|
||
|
href="https://www.cliki.net/site/feed/article.atom?title=Named%20multiple-values">
|
||
|
<link rel="stylesheet" href="static/css/style.css">
|
||
|
<link rel="stylesheet" href="static/css/colorize.css">
|
||
|
</head>
|
||
|
|
||
|
<body>
|
||
|
<span class="hidden">CLiki - Named multiple-values</span>
|
||
|
<div id="content"><div id="content-area"><div id="article-title">Named multiple-values</div><div id="article">Named-multiple-values is a piece of macrology that enables one to deal with
|
||
|
Common Lisp's multiple-values somewhat abstractly. That is, you may
|
||
|
refer to a set of values by name, and also particular values by name.<p><a href="http://web.archive.org/web/20070205214300/http://www.cs.uit.no/~frodef/sw/named-multiple-values.lisp">
|
||
|
named-multiple-values.lisp</a> might be
|
||
|
considered to be a <a href="convenience library.html" class="category">convenience library</a>. It defines a single macro, called
|
||
|
<em>named-multiple-values:define-named-multiple-values</em>. This
|
||
|
macro generates two macros, whose names it returns. See the example.<p><h4>Example</h4><p>Here is an example of what it can look like:<p><pre>
|
||
|
(define-named-multiple-values foo-values (bar zot zut)
|
||
|
(:all &all))
|
||
|
=> FOO-VALUES, FOO-VALUES-BIND
|
||
|
|
||
|
(foo-values () :bar 'barney :zut 'zutteklut)
|
||
|
=> BARNEY, NIL, ZUTTEKLUT
|
||
|
|
||
|
(foo-values-bind (&zut my-zut &bar local-bar)
|
||
|
(values 1 2 3)
|
||
|
(list my-zut local-bar))
|
||
|
=> (3 1)
|
||
|
|
||
|
(foo-values-bind (&all default-foos)
|
||
|
(values 1 2 3)
|
||
|
(foo-values (:defaults default-foos)
|
||
|
:zot 'special-zot))
|
||
|
=> 1, SPECIAL-ZOT, 3
|
||
|
</pre><p>However, named-multiple-values is not expected to be particularly
|
||
|
useful for interactive work, see the "background" section.<p><h4>Description</h4><p>I will offer two analogies to the concept of named-multiple-values:<p><ul>
|
||
|
<li> Multiple-values are similar to functions' argument-lists, where the
|
||
|
latter is "down-stream", and the former is "up-stream" flow of
|
||
|
information. Much like Common Lisp---by use of <a href="https://www.cliki.net/site/HyperSpec/Body/sec_3-4-1.html" class="hyperspec">&key</a>---allow you to
|
||
|
pass arguments (down-stream) to functions by name rather than position,
|
||
|
you can now generate up-stream multiple-values by name. </li><p> <li> Common Lisp's <a href="https://www.cliki.net/site/HyperSpec/Body/mac_defstruct.html" class="hyperspec">defstruct</a> offers the possibility of defining
|
||
|
abstract types whose concrete type is (for example) a list.
|
||
|
So while the type abstractly consits only of members a, b, and c,
|
||
|
in reality there will be a list of length three, whose first
|
||
|
element is a, second element is b, and third is c.
|
||
|
Named multiple-values are very much the same thing, except
|
||
|
the set of values only exists as an abstract entity: No list
|
||
|
or anything else is ever consed up, since the value-sets as such
|
||
|
never exist as lisp objects. Consequently, they cannot be passed
|
||
|
around between functions. </li>
|
||
|
</ul>
|
||
|
<h4>Background</h4><p>What is named-multiple-values useful for? I'm not quite sure. This
|
||
|
code is factored out from a project where I had many tens of functions
|
||
|
that followed the same protocol, both down-stream and up-stream. That
|
||
|
is, the functions accepted the same parameters, and returned the same
|
||
|
values. Being the lisper that I am, I took advantage of this
|
||
|
regularity in the code by having macros that dealt with both
|
||
|
directions of the protocol. This added greatly to the clarity of the
|
||
|
code, and saved me a lot of editing.<p>While the down-stream part of my original project's macrology was
|
||
|
rather trivial, and didn't really provide that much beyond what &key
|
||
|
already does, the up-stream part I thought might be more interesting
|
||
|
for more general-purpose use. So here it is, available as finished
|
||
|
code, or just as and idea you might find usable in your own code.<p>Because this code is factored out and modified quite a bit from my
|
||
|
original project with well-tested code, and abstracted into what I
|
||
|
suppose might be called a higher-order-macro (a macro-writing macro,
|
||
|
that is), I fully expect there to be bugs and quirks. So this code
|
||
|
should perhaps be considered more as a description of an idea than a
|
||
|
ready-to-use library.<p>Comments are welcome.<p>I (<a href="Gary King.html" class="internal">Gary King</a>) am not sure that I follow but are named-multiple-values similar to returning property lists but without the consing? <p>Yes, I think that would describe it reasonably. If you want to return numerous values and would prefer to refer to each value by name rather than ordering, you can use ``Named multiple-values''.<p><hr>
|
||
|
<a href="macro example.html" class="category">macro example</a></div></div>
|
||
|
<div id="footer" class="buttonbar"><ul><li><a href="Named multiple-values.html">Current version</a></li>
|
||
|
<li><a href="https://www.cliki.net/site/history?article=Named%20multiple-values">History</a></li>
|
||
|
<li><a href="https://www.cliki.net/site/backlinks?article=Named%20multiple-values">Backlinks</a></li><li><a href="https://www.cliki.net/site/edit-article?title=Named%20multiple-values&from-revision=3822925878">Edit</a></li><li><a href="https://www.cliki.net/site/edit-article?create=t">Create</a></li></ul></div>
|
||
|
</div>
|
||
|
<div id="header-buttons" class="buttonbar">
|
||
|
<ul>
|
||
|
<li><a href="https://www.cliki.net/">Home</a></li>
|
||
|
<li><a href="https://www.cliki.net/site/recent-changes">Recent Changes</a></li>
|
||
|
<li><a href="CLiki.html">About</a></li>
|
||
|
<li><a href="Text Formatting.html">Text Formatting</a></li>
|
||
|
<li><a href="https://www.cliki.net/site/tools">Tools</a></li>
|
||
|
</ul>
|
||
|
<div id="search">
|
||
|
<form action="https://www.cliki.net/site/search">
|
||
|
<label for="search_query" class="hidden">Search CLiki</label>
|
||
|
<input type="text" name="query" id="search_query" value="" />
|
||
|
<input type="submit" value="search" />
|
||
|
</form>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div id="pageheader">
|
||
|
<div id="header">
|
||
|
<span id="logo">CLiki</span>
|
||
|
<span id="slogan">the common lisp wiki</span>
|
||
|
<div id="login"><form method="post" action="https://www.cliki.net/site/login">
|
||
|
<label for="login_name" class="hidden">Account name</label>
|
||
|
<input type="text" name="name" id="login_name" class="login_input" />
|
||
|
<label for= "login_password" class="hidden">Password</label>
|
||
|
<input type="password" name="password" id="login_password" class="login_input" />
|
||
|
<input type="submit" name="login" value="login" id="login_submit" /><br />
|
||
|
<div id="register"><a href="https://www.cliki.net/site/register">register</a></div>
|
||
|
<input type="submit" name="reset-pw" value="reset password" id="reset_pw" />
|
||
|
</form>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body></html>
|