Page 1 of 1

Suggestion: (type or (type?

Posted: Fri Jan 10, 2003 8:40 pm
by 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.

Posted: Tue Jan 21, 2003 6:50 pm
by Lutz
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