[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] case vs. eq? - just curious ....

From: Peter Bex
Subject: Re: [Chicken-users] case vs. eq? - just curious ....
Date: Sat, 3 Mar 2012 14:53:06 +0100
User-agent: Mutt/

On Fri, Mar 02, 2012 at 06:57:56PM -0700, Matt Welland wrote:
> I'm trying to convert a comparison operator to the equivalent text. These
> are not symbols. I think case treats the target list as if it was created
> with a quote: '(>). Thus things are all symbols in the list and the eqv?
> returns false.
> > (case > ((>) "yup")(else "nope"))
> "nope"
> > (cond ((eqv? > >) "yup")(else "nope"))
> "yup"
> The cond does what I need just fine, I just didn't understand why case
> didn't also work.

Like others have said before, case implicitly quotes its arguments,
comparing the input to symbols.  Your cond is doing something entirely
different; taking the value of the identifiers and comparing the input to

> Is there a more schemeish way to convert any of the operators to the text
> representation?
> (define gt >)
> (op->string gt)
>  => ">"

I think you're perhaps confused about the difference between one of many
possible textual representations of a program and its semantical meaning.

How is Scheme supposed to know the "canonical" name of a random lambda?
The only way is whatever arbitrarily chosen assignment you've decided on
in your program.  Having said that, you can extract the "initial" name
of a procedure, which is the name of the surrounding define when the
lambda was first introduced (or its C name in some cases):

#;1> (define (bar x) 1)
#;2> (define foo (lambda (y) y))
#;3> (let ((whatever (lambda (z) z)))
       ;; set! global value
       (set! qux whatever))
#;4> (##sys#lambda-info->string (##sys#lambda-info bar))
"(bar x)"
#;5> (##sys#lambda-info->string (##sys#lambda-info foo))
"(foo y)"
#;6> (##sys#lambda-info->string (##sys#lambda-info qux))
"(whatever z)"
#;7> (##sys#lambda-info->string (##sys#lambda-info +))
#;8> ;; But look at this:
(use numbers)
#;9> (##sys#lambda-info->string (##sys#lambda-info +))
"(numbers#+ . args176)"

As you can see, this "canonical" name isn't something you
can easily influence.  Another way to solve this particilar
issue is to make an alist or hash table mapping your desired
canonical names to procedures, and op->string look up names
in this table.  A macro might also help in reducing work,
as it allows you to list just the "canonical" names of
procedures once and have it generate a case statement:

(define-syntax define-op-converter
  (syntax-rules ()
    ((_ name op0 ...)
     (define (name op)
       (select op
         ((op0) (symbol->string 'op0))
         (else (error "unknown operator" op)))))))
#;2> (define-op-converter op->string > < + -)
#;3> (op->string >)
#;4> ;; But there are many limitations:
(use numbers)
#;5> (op->string >)
Error: unknown operator: #<procedure (numbers#> x11875 x21876 . xs1877)>

Depending on what exactly you're trying to do in the bigger
picture, perhaps rethinking your approach is a better solution
than trying to work around this.

"The process of preparing programs for a digital computer
 is especially attractive, not only because it can be economically
 and scientifically rewarding, but also because it can be an aesthetic
 experience much like composing poetry or music."
                                                        -- Donald Knuth

reply via email to

[Prev in Thread] Current Thread [Next in Thread]