Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 918
Prethia Avatar asked Mar 18 '23 14:03

Prethia


2 Answers

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.)

like image 64
Chris Jester-Young Avatar answered Mar 23 '23 13:03

Chris Jester-Young


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.

like image 34
Chris Vine Avatar answered Mar 23 '23 12:03

Chris Vine