Defining a conda in a schema

This will be an easy question, which I am guessing, but I need it. I am making a simulator game in a circuit (Dr Racket) and I want to change how cond works. But to change the cond situation, I need to know the definition from cond and I couldn't find it in Dr racket. Can someone provide a definition for cond in the schema?

+3


source to share


2 answers


The definition of a rocket cond

is in collects/racket/private/cond.rkt

. It is written using low-level syntax syntax operations, not using either syntax-rules

or syntax-case

, so if you don't know syntax objects very well, it won't be readable to you.

As an alternative starting place for a configured cond

one, one definition cond

is the reference implementation given in SRFI 61 . It's concise and one of the best implementations cond

I've seen:



(define-syntax cond
  (syntax-rules (=> else)

    ((cond (else else1 else2 ...))
     ;; The (if #t (begin ...)) wrapper ensures that there may be no
     ;; internal definitions in the body of the clause.  R5RS mandates
     ;; this in text (by referring to each subform of the clauses as
     ;; <expression>) but not in its reference implementation of cond,
     ;; which just expands to (begin ...) with no (if #t ...) wrapper.
     (if #t (begin else1 else2 ...)))

    ((cond (test => receiver) more-clause ...)
     (let ((t test))
       (cond/maybe-more t
                        (receiver t)
                        more-clause ...)))

    ((cond (generator guard => receiver) more-clause ...)
     (call-with-values (lambda () generator)
       (lambda t
         (cond/maybe-more (apply guard    t)
                          (apply receiver t)
                          more-clause ...))))

    ((cond (test) more-clause ...)
     (let ((t test))
       (cond/maybe-more t t more-clause ...)))

    ((cond (test body1 body2 ...) more-clause ...)
     (cond/maybe-more test
                      (begin body1 body2 ...)
                      more-clause ...))))

(define-syntax cond/maybe-more
  (syntax-rules ()
    ((cond/maybe-more test consequent)
     (if test
         consequent))
    ((cond/maybe-more test consequent clause ...)
     (if test
         consequent
         (cond clause ...)))))

      

(Since it is pleasing, please call your version other than cond

to avoid confusion.)

+4


source


r5rs describes the cond here: http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-ZH-7.html#% _sec_4.2.1



Typically you will implement it as a macro.

+1


source







All Articles