80 lines
5.4 KiB
HTML
80 lines
5.4 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<title>CLiki: bind</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=bind">
|
||
|
<link rel="stylesheet" href="static/css/style.css">
|
||
|
<link rel="stylesheet" href="static/css/colorize.css">
|
||
|
</head>
|
||
|
|
||
|
<body>
|
||
|
<span class="hidden">CLiki - bind</span>
|
||
|
<div id="content"><div id="content-area"><div id="article-title">bind</div><div id="article">Bind combines <a href="https://www.cliki.net/site/HyperSpec/Body/speope_letcm_letst.html" class="hyperspec">let*</a>, <a href="https://www.cliki.net/site/HyperSpec/Body/mac_destructuring-bind.html" class="hyperspec">destructuring-bind</a> and <a href="https://www.cliki.net/site/HyperSpec/Body/mac_multiple-value-bind.html" class="hyperspec">multiple-value-bind</a> into a single form. Simple bindings are as in let*. Destructuring is done if the first item in a binding is a list. Multiple value binding is done if the first item in a binding is a list and the first item in the list is 'values'. More information about bind can be found on <a href="http://common-lisp.net/project/metabang-bind/">common-lisp.net</a>. <p>An example is probably the best way to describe its syntax:
|
||
|
<pre>
|
||
|
(bind ((a 2)
|
||
|
((b &rest args &key (c 2) &allow-other-keys) '(:a :c 5 :d 10 :e 54))
|
||
|
((:values d e) (truncate 4.5)))
|
||
|
(list a b c d e args))
|
||
|
==> (2 :A 5 4 0.5 (:C 5 :D 10 :E 54))
|
||
|
</pre>
|
||
|
Bind is especially handy when you have more than one layer of
|
||
|
multiple-value-bind or destructuring-bind. Since bind is a single form,
|
||
|
you don't end up too far off to the right in editor land.<p>Bind works by parsing the bindings and rewriting them as nested
|
||
|
let, multiple-value-bind and destructuring-bind forms. Bind handles
|
||
|
declarations correctly -- putting each at the appropriate level.<p>Bind is released under the MIT license. The most recent version (as of 1 October 2005) reflects this explicitly.<p>I like bind because it (a). presents a uniform means of three of CL's most typical binding constructs and (b). because you don't have to worry about nesting! I like the more explicit syntax with "values" because it makes it clear what it is doing and is analogous to something like (setf (values a b) (foo)). <p>Bind was written by <a href="Gary King.html" class="internal">Gary King</a>.<p>An alternative syntax -- which bind does not support -- that doesn't rely on explicitly saying "values" is:
|
||
|
<pre>
|
||
|
(bind ((d e (truncate 4.5)))
|
||
|
(list d e))
|
||
|
=>
|
||
|
(4 0.5)
|
||
|
</pre>
|
||
|
Implementation-wise, what it's doing is using the last item in the
|
||
|
list, (truncate 4.5), as the form to evaluate. Then the first item, d,
|
||
|
gets the primary value, and each item in between gets one of the
|
||
|
other values; in this case, that's just e.<p>This is particularly nice because it makes it possible to combine
|
||
|
multiple-value binding and destructuring binding. For example:<p><pre>
|
||
|
(bind (((this &rest that) the-other (values (list 1 2 3 4) 5)))
|
||
|
(list this that the-other))
|
||
|
=>
|
||
|
(1 (2 3 4) 5)
|
||
|
</pre><p>See also: <a href="X.LET-STAR.html" class="internal">X.LET-STAR</a> <a href="https://www.cliki.net/WITH%20macro" class="new">WITH macro</a>
|
||
|
<a href="pattern matching.html" class="category">pattern matching</a> <a href="utilities.html" class="category">utilities</a></div></div>
|
||
|
<div id="footer" class="buttonbar"><ul><li><a href="bind.html">Current version</a></li>
|
||
|
<li><a href="https://www.cliki.net/site/history?article=bind">History</a></li>
|
||
|
<li><a href="https://www.cliki.net/site/backlinks?article=bind">Backlinks</a></li><li><a href="https://www.cliki.net/site/edit-article?title=bind&from-revision=3686413425">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>
|