Suggestion: (type or (type?

Q&A's, tips, howto's
Locked
CaveGuy
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio
Contact:

Suggestion: (type or (type?

Post 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.
Bob the Caveguy aka Lord High Fixer.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Locked