Prethia
Prethia

Reputation: 1203

Cond definition in scheme

This will be an easy question I guess but I need it.I am making a simulator game in scheme(Dr Racket)and I want to change how cond works.But to change the thing cond does I need to know the definition of cond and I could not find it in Dr racket.Can someone give the definition of cond in scheme?

Upvotes: 4

Views: 1966

Answers (2)

C. K. Young
C. K. Young

Reputation: 223003

The Racket definition of cond is in collects/racket/private/cond.rkt. It's written using low-level syntax object operations, not using either syntax-rules nor syntax-case, so unless you know syntax objects very well, it won't be readable to you.

As an alternative starting place for your customised cond, one definition of cond is the reference implementation given in SRFI 61. It is succinct and is one of the best implementations of 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 ...)))))

(As molbdnilo says, though, please call your version something other than cond to avoid confusion.)

Upvotes: 6

Chris Vine
Chris Vine

Reputation: 727

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

You would normally implement it as a macro.

Upvotes: 1

Related Questions