Suggestion: (type or (type?

Started by CaveGuy, January 10, 2003, 12:40:20 PM

Previous topic - Next topic

CaveGuy

X-Lisp deviants<sp?> have a function (type symbol)

that returns SYMBOL STRING REAL LIST ....



It allows a case statement to be used to handle a wide range of input presorted processing by data type.



(define (data2string x)

   (case (type x)

      (LIST (append "List: " (string x)))

      (REAL (append "Real: " (string x)))

      (SYMBOL (append "Symbol: " (string x)))

      (STRING (append "String: " (string x)))

      ...

      (true (append "Undefined: (string x)))))



I know I can use:

(cond ((list? x) ......

         ((NaN? x) .......



But sometimes I just want to know what a symbol

is pointing to.
Bob the Caveguy aka Lord High Fixer.

Lutz

#1
you can use the (undocumented) function dump to retrieve the contents of a lisp cell the second number is the type:



(define (type x)  (& 15 (nth 1 (dump x))))



(type nil) => 0

(type true) => 1

(type 123) => 2 ;; integer

(type 1.23) => 3 ;; float

(type "abcd") => 4 ;; string

(type 'asymbol) => 5 ;; symbol

(type MAIN) => 6 ;; context

(type +) => 7   ;; 8 for imports

(type ''asym) => 9 ;; quote

(type '(1 2 3)) => 10 ;; list expression

(type type) => 11 ;; lambda 12 macro



See the file newlisp.h in the source distribution for other higher bits and their meaning.



Lutz