chicken-users
[Top][All Lists]
Advanced

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

Re: Question about how to check a symbol is bound


From: Lassi Kortela
Subject: Re: Question about how to check a symbol is bound
Date: Fri, 23 Jun 2023 11:22:41 +0300
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.12.0

  (define-record egg-info
    name author desc)

  (define (show-egg-info egg)
    (define (symbol-value sym)
      (##sys#slot sym 0))
    (define (getter field-name)
      (symbol-value
       (string->symbol
        (format #f "egg-info-~a"
                field-name))))
    (let ((fields '(name author desc)))
      (for-each
       (lambda (f)
         (format #t "~a: ~a~%"
                 f
                 ((getter f) egg)))
       fields)))

  (show-egg-info (make-egg-info
                  "F-operator"
                  "Kon Lovett"
                  "Shift/Reset Control Operators"))

What you're looking for is record inspection (aka introspection). That has been standardized in R6RS, but Chicken doesn't have it AFAIK.

The right points of comparison in Common Lisp are:

* slot-value
* slot-boundp
* slot-exists-p

Symbol-value is not a good point of comparison, as it has to do with packages, not records. Neither Scheme not CL programmers tend to solve problems by poking around the symbol table. It's a last resort.

Your best bet in Scheme as it stands, is to use a hash table or association list instead of a record type. Or wrap a hash table in a record.

I think it is a very common idiom in languages from Lisp family. So it is important to know
how to check symbol is bound and get its value.

It's not idiomatic at all. It's useful mainly for tools that support interactive development, e.g. via REPL or Emacs.



reply via email to

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