1
0
Fork 0
cl-sites/novaspec.org/cl/f_sublis.html
2025-02-05 18:52:26 +01:00

479 lines
No EOL
10 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>sublis, nsublis | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_sublis.html"
><LINK REL="next" HREF="f_subst.html" TYPE="text/html" TITLE="subst, subst-if, subst-if-not, nsubst, nsubst-if, nsubst-if-not"
><LINK REL="prev" HREF="f_copy-tree.html" TYPE="text/html" TITLE="copy-tree"
><LINK REL="up" HREF="14_2_Conses_Dictionary.html" TYPE="text/html" TITLE="14.2 Conses Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="14_Conses.html"
>14. Conses</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="14_2_Conses_Dictionary.html"
>14.2 Conses Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_sublis.html"
>sublis, nsublis</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_copy-tree.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="f_subst.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="sublis, nsublis"
></SPAN
><B
>sublis, nsublis</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Function</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><P CLASS="j"
><B
>sublis</B
> <SPAN CLASS="cmssi"
>alist</SPAN
> <SPAN CLASS="cmssi"
>tree</SPAN
> <SPAN CLASS="cmtt"
>&amp;key</SPAN
> <SPAN CLASS="cmssi"
>key</SPAN
> <SPAN CLASS="cmssi"
>test</SPAN
> <SPAN CLASS="cmssi"
>test-not</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>new-tree</SPAN
></P
><P CLASS="j"
><B
>nsublis</B
> <SPAN CLASS="cmssi"
>alist</SPAN
> <SPAN CLASS="cmssi"
>tree</SPAN
> <SPAN CLASS="cmtt"
>&amp;key</SPAN
> <SPAN CLASS="cmssi"
>key</SPAN
> <SPAN CLASS="cmssi"
>test</SPAN
> <SPAN CLASS="cmssi"
>test-not</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>new-tree</SPAN
></P
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>alist</VAR
> &#8212; an <A HREF="26_1_Glossary.html#association_list"
><EM CLASS="term"
>association list</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>tree</VAR
> &#8212; a <A HREF="26_1_Glossary.html#tree"
><EM CLASS="term"
>tree</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>test</VAR
> &#8212; a <A HREF="26_1_Glossary.html#designator"
><EM CLASS="term"
>designator</EM
></A
> for a <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> of two <A HREF="26_1_Glossary.html#argument"
><EM CLASS="term"
>arguments</EM
></A
> that returns a <A HREF="26_1_Glossary.html#generalized_boolean"
><EM CLASS="term"
>generalized boolean</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>test-not</VAR
> &#8212; a <A HREF="26_1_Glossary.html#designator"
><EM CLASS="term"
>designator</EM
></A
> for a <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> of two <A HREF="26_1_Glossary.html#argument"
><EM CLASS="term"
>arguments</EM
></A
> that returns a <A HREF="26_1_Glossary.html#generalized_boolean"
><EM CLASS="term"
>generalized boolean</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>key</VAR
> &#8212; a <A HREF="26_1_Glossary.html#designator"
><EM CLASS="term"
>designator</EM
></A
> for a <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> of one argument, or <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. </P
><P CLASS="j"
><VAR CLASS="param"
>new-tree</VAR
> &#8212; a <A HREF="26_1_Glossary.html#tree"
><EM CLASS="term"
>tree</EM
></A
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="f_sublis.html" CLASS="funref"
><B
>sublis</B
></A
> makes substitutions for <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>objects</EM
></A
> in <VAR CLASS="param"
>tree</VAR
> (a structure of <A HREF="26_1_Glossary.html#cons"
><EM CLASS="term"
>conses</EM
></A
>). <A HREF="f_sublis.html" CLASS="funref"
><B
>nsublis</B
></A
> is like <A HREF="f_sublis.html" CLASS="funref"
><B
>sublis</B
></A
> but destructively modifies the relevant parts of the <VAR CLASS="param"
>tree</VAR
>. </P
><P CLASS="j"
><A HREF="f_sublis.html" CLASS="funref"
><B
>sublis</B
></A
> looks at all subtrees and leaves of <VAR CLASS="param"
>tree</VAR
>; if a subtree or leaf appears as a key in <VAR CLASS="param"
>alist</VAR
> (that is, the key and the subtree or leaf <A HREF="26_1_Glossary.html#satisfy_the_test"
><EM CLASS="term"
>satisfy the test</EM
></A
>), it is replaced by the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> with which that key is associated. This operation is non-destructive. In effect, <A HREF="f_sublis.html" CLASS="funref"
><B
>sublis</B
></A
> can perform several <A HREF="f_subst.html" CLASS="funref"
><B
>subst</B
></A
> operations simultaneously. </P
><P CLASS="j"
>If <A HREF="f_sublis.html" CLASS="funref"
><B
>sublis</B
></A
> succeeds, a new copy of <VAR CLASS="param"
>tree</VAR
> is returned in which each occurrence of such a subtree or leaf is replaced by the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> with which it is associated. If no changes are made, the original tree is returned. The original <VAR CLASS="param"
>tree</VAR
> is left unchanged, but the result tree may share cells with it. </P
><P CLASS="j"
><A HREF="f_sublis.html" CLASS="funref"
><B
>nsublis</B
></A
> is permitted to modify <VAR CLASS="param"
>tree</VAR
> but otherwise returns the same values as <A HREF="f_sublis.html" CLASS="funref"
><B
>sublis</B
></A
>.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
> (sublis '((x . 100) (z . zprime))
'(plus x (minus g z x p) 4 . x))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (PLUS 100 (MINUS G ZPRIME 100 P) 4 . 100)
(sublis '(((+ x y) . (- x y)) ((- x y) . (+ x y)))
'(* (/ (+ x y) (+ x p)) (- x y))
:test #'equal)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (* (/ (- X Y) (+ X P)) (+ X Y))
(setq tree1 '(1 (1 2) ((1 2 3)) (((1 2 3 4)))))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 (1 2) ((1 2 3)) (((1 2 3 4))))
(sublis '((3 . "three")) tree1)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 (1 2) ((1 2 "three")) (((1 2 "three" 4))))
(sublis '((t . "string"))
(sublis '((1 . "") (4 . 44)) tree1)
:key #'stringp)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ("string" ("string" 2) (("string" 2 3)) ((("string" 2 3 44))))
tree1 <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 (1 2) ((1 2 3)) (((1 2 3 4))))
(setq tree2 '("one" ("one" "two") (("one" "Two" "three"))))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ("one" ("one" "two") (("one" "Two" "three")))
(sublis '(("two" . 2)) tree2)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ("one" ("one" "two") (("one" "Two" "three")))
tree2 <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ("one" ("one" "two") (("one" "Two" "three")))
(sublis '(("two" . 2)) tree2 :test 'equal)
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ("one" ("one" 2) (("one" "Two" "three")))
(nsublis '((t . 'temp))
tree1
:key #'(lambda (x) (or (atom x) (&lt; (list-length x) 3))))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((QUOTE TEMP) (QUOTE TEMP) QUOTE TEMP)</PRE
></DD
><DT
><B
>Side Effects</B
></DT
><DD
><P CLASS="j"
><A HREF="f_sublis.html" CLASS="funref"
><B
>nsublis</B
></A
> modifies <VAR CLASS="param"
>tree</VAR
>.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_subst.html" CLASS="funref"
><B
>subst</B
></A
>, <A HREF="3_2_Compilation.html#sec_3_2_1" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>3.2.1</SPAN
> <SPAN CLASS="cmr"
>(Compiler</SPAN
> <SPAN CLASS="cmr"
>Terminology)</SPAN
></A
>, <A HREF="3_6_Traversal_Rules_and_Side_Effects.html#sec_3_6" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>3.6</SPAN
> <SPAN CLASS="cmr"
>(Traversal</SPAN
> <SPAN CLASS="cmr"
>Rules</SPAN
> <SPAN CLASS="cmr"
>and</SPAN
> <SPAN CLASS="cmr"
>Side</SPAN
> <SPAN CLASS="cmr"
>Effects)</SPAN
></A
></P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
>The <SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:test-not</SPAN
></SPAN
> parameter is deprecated. </P
><P CLASS="j"
>Because the side-effecting variants (<I CLASS="i"
><I
>e.g.</I
></I
>, <A HREF="f_sublis.html" CLASS="funref"
><B
>nsublis</B
></A
>) potentially change the path that is being traversed, their effects in the presence of shared or circular structure structure may vary in surprising ways when compared to their non-side-effecting alternatives. To see this, consider the following side-effect behavior, which might be exhibited by some implementations:</P
><PRE CLASS="screen"
>(defun test-it (fn)
(let* ((shared-piece (list 'a 'b))
(data (list shared-piece shared-piece)))
(funcall fn '((a . b) (b . a)) data)))
(test-it #'sublis) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((B A) (B A))
(test-it #'nsublis) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((A B) (A B))</PRE
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_copy-tree.html" CLASS="prev"
>&#8592;</A
><A HREF="f_subst.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>