Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know CL well enough, but will Scheme macros do? Here's a while loop in Scheme:</p> <pre><code>(define-syntax while (syntax-rules () ((while pred body ...) (let loop () (if pred (begin body ... (loop))))))) </code></pre> <p>In this case, the example demonstrates that you can easily write your own control structures using macros. <a href="http://mumble.net/~campbell/scheme/foof-loop.txt" rel="nofollow"><code>foof-loop</code></a> is a collection of even more useful looping constructs (probably nothing new given CL's ones, but still good for a demonstration).</p> <hr> <p>Another use case: picking values out of associative lists. Say users pass in an alist as options to your function. You can easily pick values out by using this macro:</p> <pre><code>(define-syntax let-assq (syntax-rules () ((let-assq alist (key) body ...) (let ((key (assq-ref alist 'key))) body ...)) ((let-assq alist (key rest ...) body ...) (let ((key (assq-ref alist 'key))) (let-assq alist (rest ...) body ...))))) ;; Guile built-in (define (assq-ref alist key) (cond ((assq key alist) =&gt; cdr) (else #f))) </code></pre> <p>Example usage:</p> <pre><code>(define (binary-search tree needle (lt? &lt;)) (let loop ((node tree)) (and node (let-assq node (value left right) (cond ((lt? needle value) (loop left)) ((lt? value needle) (loop right)) (else value)))))) </code></pre> <p>Notice how the <code>let-assq</code> macro allows picking out the <code>value</code>, <code>left</code>, and <code>right</code> keys from the "node" without having to write a much longer <code>let</code> form.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload