I define a function
(define 1-9 (list->set (range 1 10)))
I want to see if 1-9
is really a set. How can I get the type of 1-9
?
I tried to google racket check type, but I can't find any useful information.
I define a function
(define 1-9 (list->set (range 1 10)))
I want to see if 1-9
is really a set. How can I get the type of 1-9
?
I tried to google racket check type, but I can't find any useful information.
#lang racket
is dynamically typed. Practically speaking, this means you normally do not (should not) care about "The" "Type" of some value.
Instead (as Alex pointed out), you give a value to a "predicate" function like list?
. If the predicate returns true, then you may go ahead and do list
-y things with the value -- give the value to functions that expect list
.
This is much more useful and reliable than having something like (typeof value)
that returns magic symbols like List
. After all, what you care about is what you can do with the value. A predicate tells you that. And a predicate allows for values that can be used in more than one way (e.g. as a list and as a set, both).
p.s. This is similar to why version numbers (like Semantic Versioning) are so silly. Given some installed library, what you actually care about is, does it provide certain functions and behavior. You want to ask the actual installed library, do you provide function X -- not use some magic number and outside information to guess.
p.p.s. What if you want to serialize values (write and read them to a file)? You do need to choose a way to represent each value. In Racket one approach is to use the printed representation of primitive values, and something like prefab structs for others -- then use write
and read
. There is also racket/serialize
. In any case, serializing values is a relatively rare thing to do.
© 2022 - 2024 — McMap. All rights reserved.
generic-set?
. As in(generic-set? 1-9) = #true
– Tyroliennegeneric-set?
. If you want to see if its a list, uselist?
. If you want to see if its a number, usenumber?
. In Racket types can overlap however, so something could be both ageneric-set?
and alist?
, and that's perfectly fine, just like it's fine for something to be both anumber?
and aninteger?
. Any value has the "type"any/c
, since(any/c x)
is true no matter whatx
is.5
has at least three "types"any/c
,number?
, andinteger?
. So can you clarify what you actually want, and why predicates likegeneric-set?
aren't enough? – TyroliennetypeOf :: ∀ a. (Typeable a) => a -> TypeRep
, though you're right thatTypeable a
does not allow an arbitrary type, and because of that parametricity doesn't say much about it. I guess parametricity would say just as much about that as it would about∀ a. (a -> TypeRep) -> a -> TypeRep
– Tyrolienne