| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| 2 |
<html> |
| 3 |
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ --> |
| 4 |
<head> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 6 |
<title>Examples of ALWAYS (ANSI and GNU Common Lisp Document)</title> |
| 7 |
|
| 8 |
<meta name="description" content="Examples of ALWAYS (ANSI and GNU Common Lisp Document)"> |
| 9 |
<meta name="keywords" content="Examples of ALWAYS (ANSI and GNU Common Lisp Document)"> |
| 10 |
<meta name="resource-type" content="document"> |
| 11 |
<meta name="distribution" content="global"> |
| 12 |
<meta name="Generator" content="makeinfo"> |
| 13 |
<link href="index.html" rel="start" title="Top"> |
| 14 |
<link href="Termination-Test-Clauses.html" rel="up" title="Termination Test Clauses"> |
| 15 |
<link href="Examples-of-WHILE-and-UNTIL-clauses.html" rel="next" title="Examples of WHILE and UNTIL clauses"> |
| 16 |
<link href="Examples-of-REPEAT-clause.html" rel="prev" title="Examples of REPEAT clause"> |
| 17 |
<style type="text/css"> |
| 18 |
<!-- |
| 19 |
a.summary-letter {text-decoration: none} |
| 20 |
blockquote.indentedblock {margin-right: 0em} |
| 21 |
div.display {margin-left: 3.2em} |
| 22 |
div.example {margin-left: 3.2em} |
| 23 |
div.lisp {margin-left: 3.2em} |
| 24 |
kbd {font-style: oblique} |
| 25 |
pre.display {font-family: inherit} |
| 26 |
pre.format {font-family: inherit} |
| 27 |
pre.menu-comment {font-family: serif} |
| 28 |
pre.menu-preformatted {font-family: serif} |
| 29 |
span.nolinebreak {white-space: nowrap} |
| 30 |
span.roman {font-family: initial; font-weight: normal} |
| 31 |
span.sansserif {font-family: sans-serif; font-weight: normal} |
| 32 |
ul.no-bullet {list-style: none} |
| 33 |
--> |
| 34 |
</style> |
| 35 |
|
| 36 |
|
| 37 |
</head> |
| 38 |
|
| 39 |
<body lang="en"> |
| 40 |
<span id="Examples-of-ALWAYS"></span><div class="header"> |
| 41 |
<p> |
| 42 |
Next: <a href="Examples-of-WHILE-and-UNTIL-clauses.html" accesskey="n" rel="next">Examples of WHILE and UNTIL clauses</a>, Previous: <a href="Examples-of-REPEAT-clause.html" accesskey="p" rel="prev">Examples of REPEAT clause</a>, Up: <a href="Termination-Test-Clauses.html" accesskey="u" rel="up">Termination Test Clauses</a> </p> |
| 43 |
</div> |
| 44 |
<hr> |
| 45 |
<span id="Examples-of-ALWAYS_002c-NEVER_002c-and-THEREIS-clauses"></span><h4 class="subsubsection">6.1.4.2 Examples of ALWAYS, NEVER, and THEREIS clauses</h4> |
| 46 |
|
| 47 |
<div class="example"> |
| 48 |
<pre class="example">;; Make sure I is always less than 11 (two ways). |
| 49 |
;; The FOR construct terminates these loops. |
| 50 |
(loop for i from 0 to 10 |
| 51 |
always (< i 11)) |
| 52 |
⇒ T |
| 53 |
(loop for i from 0 to 10 |
| 54 |
never (> i 11)) |
| 55 |
⇒ T |
| 56 |
|
| 57 |
;; If I exceeds 10 return I; otherwise, return NIL. |
| 58 |
;; The THEREIS construct terminates this loop. |
| 59 |
(loop for i from 0 |
| 60 |
thereis (when (> i 10) i) ) |
| 61 |
⇒ 11 |
| 62 |
|
| 63 |
;;; The FINALLY clause is not evaluated in these examples. |
| 64 |
(loop for i from 0 to 10 |
| 65 |
always (< i 9) |
| 66 |
finally (print "you won't see this")) |
| 67 |
⇒ NIL |
| 68 |
(loop never t |
| 69 |
finally (print "you won't see this")) |
| 70 |
⇒ NIL |
| 71 |
(loop thereis "Here is my value" |
| 72 |
finally (print "you won't see this")) |
| 73 |
⇒ "Here is my value" |
| 74 |
|
| 75 |
;; The FOR construct terminates this loop, so the FINALLY clause |
| 76 |
;; is evaluated. |
| 77 |
(loop for i from 1 to 10 |
| 78 |
thereis (> i 11) |
| 79 |
finally (prin1 'got-here)) |
| 80 |
<tt> |> </tt> GOT-HERE |
| 81 |
⇒ NIL |
| 82 |
|
| 83 |
;; If this code could be used to find a counterexample to Fermat's |
| 84 |
;; last theorem, it would still not return the value of the |
| 85 |
;; counterexample because all of the THEREIS clauses in this example |
| 86 |
;; only return T. But if Fermat is right, that won't matter |
| 87 |
;; because this won't terminate. |
| 88 |
|
| 89 |
(loop for z upfrom 2 |
| 90 |
thereis |
| 91 |
(loop for n upfrom 3 below (log z 2) |
| 92 |
thereis |
| 93 |
(loop for x below z |
| 94 |
thereis |
| 95 |
(loop for y below z |
| 96 |
thereis (= (+ (expt x n) (expt y n)) |
| 97 |
(expt z n)))))) |
| 98 |
</pre></div> |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
</body> |
| 104 |
</html> |