Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT: In response to your update, I think you want to do something like this:</p> <pre><code>(eval `(define ,&lt;procedure-to-return-symbol&gt; ,&lt;value&gt;) &lt;environment&gt;) </code></pre> <p>e.g. in mit-scheme:</p> <pre><code>(eval `(define ,(string-&gt;symbol "abc") ,(* 2 2)) user-initial-environment) </code></pre> <p>Where <code>user-initial-environment</code> is the environment under which symbols are interned when typed into the REPL. This example will return the symbol <code>abc</code> associated with the value 4. Using this method you would be able to use your procedure to create the name and associate it with the value returned by my solution below. You can read more about <code>eval</code> and mit-scheme environments <a href="http://groups.csail.mit.edu/mac/ftpdir/scheme-7.4/doc-html/scheme_14.html" rel="nofollow">here</a>. <code>&lt;/edit&gt;</code></p> <p>EDIT2: An explicit solution:</p> <pre><code>(define (cxr x) (define (helper xlist arg) (cond ((null? xlist) arg) ((eq? (car xlist) #\a) (car (helper (cdr xlist) arg))) ((eq? (car xlist) #\d) (cdr (helper (cdr xlist) arg))) (else (error "INVALID ARGS FOR CXR")))) (eval `(define ,(string-&gt;symbol (string-append "c" x "r")) ,(lambda (arg) (helper (string-&gt;list x) arg))) user-initial-environment)) </code></pre> <p>In this way you can create named procedures for any depth of "ad" strings.<code>&lt;/edit&gt;</code></p> <p>Your initial solution is at best usable where there are compositions of <code>car</code> and <code>cdr</code> already defined. The problem is looking for a procedure that returns a procedure which takes the car/cdr of a list to an arbitrary depth. This is my solution:</p> <pre><code>(define (cxr x) (define (helper xlist arg) (cond ((null? xlist) arg) ((eq? (car xlist) #\a) (car (helper (cdr xlist) arg))) ((eq? (car xlist) #\d) (cdr (helper (cdr xlist) arg))) (else (error "INVALID ARGS FOR CXR")))) (lambda (arg) (helper (string-&gt;list x) arg))) </code></pre> <p><code>helper</code> runs down the list of a's and d's calling the either <code>car</code> or <code>cdr</code> on the result of the next call to <code>helper</code> -- it builds the body for the <code>lambda</code>. When the list is empty, <code>helper</code> returns <code>arg</code> which is the parameter for the <code>lambda</code> expression. Since the <code>lambda</code> form does not have an argument passed to it in the definition, <code>cxr</code> will return a procedure. </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.
 

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