Replace example blocks with src

This commit is contained in:
Marcus Kammer 2024-05-01 10:14:09 +02:00
parent 3d960576aa
commit f960388d0f
Signed by: marcuskammer
GPG key ID: C374817BE285268F

View file

@ -532,20 +532,20 @@ it just as you would use mapcar. For example, if you want a list of the
values returned by some function when it is applied to all the integers values returned by some function when it is applied to all the integers
from 1 to 10, you could create a new list and pass it to mapcar: from 1 to 10, you could create a new list and pass it to mapcar:
#+BEGIN_EXAMPLE #+BEGIN_SRC lisp
(mapcar fn (mapcar fn
(do* ((x 1 (1+ x)) (do* ((x 1 (1+ x))
(result (list x) (push x result))) (result (list x) (push x result)))
((= x 10) (nreverse result)))) ((= x 10) (nreverse result))))
#+END_EXAMPLE #+END_SRC
but this approach is both ugly and but this approach is both ugly and
inefficient.[fn:2] Instead you could define a new mapping inefficient.[fn:2] Instead you could define a new mapping
function map1-n (see page 54), and then call it as follows: function map1-n (see page 54), and then call it as follows:
#+BEGIN_EXAMPLE #+BEGIN_SRC lisp
(map1-n fn 10) (map1-n fn 10)
#+END_EXAMPLE #+END_SRC
Defining functions is comparatively straightforward. Macros provide a Defining functions is comparatively straightforward. Macros provide a
more general, but less well-understood, means of defining new operators. more general, but less well-understood, means of defining new operators.
@ -573,18 +573,18 @@ some body of code for x from a to b. The built-in Lisp do is meant for
more general cases. For simple iteration it does not yield the most more general cases. For simple iteration it does not yield the most
readable code: readable code:
#+BEGIN_EXAMPLE #+BEGIN_SRC lisp
(do ((x a (+ 1 x))) (do ((x a (+ 1 x)))
((> x b)) ((> x b))
(print x)) (print x))
#+END_EXAMPLE #+END_SRC
Instead, suppose we could just say: Instead, suppose we could just say:
#+BEGIN_EXAMPLE #+BEGIN_SRC lisp
(for (x a b) (for (x a b)
(print x)) (print x))
#+END_EXAMPLE #+END_SRC
Macros make this possible. With six lines of code (see page 154) we can Macros make this possible. With six lines of code (see page 154) we can
add for to the language, just as if it had been there from the start. add for to the language, just as if it had been there from the start.