Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the type of a value in Scheme?

Tags:

I want a function that gets the type of a value at runtime. Example use:

(get-type a) 

where a has been defined to be some arbitrary Scheme value.

How do I do this? Or do I have to implement this myself, using a cond stack of boolean?, number? etc. ?

like image 964
Matt Fenwick Avatar asked Jul 19 '12 18:07

Matt Fenwick


People also ask

How do I know the value type?

To check the type of a variable, you can use the type() function, which takes the variable as an input. Inside this function, you have to pass either the variable name or the value itself. And it will return the variable data type.

How do you define a variable in a Scheme?

A variable is a box-like object that can hold any Scheme value. It is said to be undefined if its box holds a special Scheme value that denotes undefined-ness (which is different from all other Scheme values, including for example #f ); otherwise the variable is defined.

What is a symbol in Scheme?

Symbols in Scheme are widely used in three ways: as items of discrete data, as lookup keys for alists and hash tables, and to denote variable references. Looking beyond how they are written, symbols are different from strings in two important respects. The first important difference is uniqueness.

How do you write not equal to in Scheme?

Since Scheme doesn't have a numeric "not equals" operator (like the != operator in C/Java/Python), we have to combine not and = in order to evaluate "not equals".


1 Answers

In Scheme implementations with a Tiny-CLOS-like object system, you can just use class-of. Here's a sample session in Racket, using Swindle:

$ racket -I swindle Welcome to Racket v5.2.1. -> (class-of 42) #<primitive-class:exact-integer> -> (class-of #t) #<primitive-class:boolean> -> (class-of 'foo) #<primitive-class:symbol> -> (class-of "bar") #<primitive-class:immutable-string> 

And similarly with Guile using GOOPS:

scheme@(guile-user)> ,use (oop goops) scheme@(guile-user)> (class-of 42) $1 = #<<class> <integer> 14d6a50> scheme@(guile-user)> (class-of #t) $2 = #<<class> <boolean> 14c0000> scheme@(guile-user)> (class-of 'foo) $3 = #<<class> <symbol> 14d3a50> scheme@(guile-user)> (class-of "bar") $4 = #<<class> <string> 14d3b40> 
like image 74
Chris Jester-Young Avatar answered Oct 13 '22 18:10

Chris Jester-Young