How to check type in Racket?
Asked Answered
P

1

10

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.

Pulsifer answered 14/11, 2018 at 1:57 Comment(6)
generic-set?. As in (generic-set? 1-9) = #trueTyrolienne
So you have to know the type before? What if I have no idea about the type of the output?Pulsifer
If you want to see if its a set, use generic-set?. If you want to see if its a list, use list?. If you want to see if its a number, use number?. In Racket types can overlap however, so something could be both a generic-set? and a list?, and that's perfectly fine, just like it's fine for something to be both a number? and an integer?. Any value has the "type" any/c, since (any/c x) is true no matter what x is. 5 has at least three "types" any/c, number?, and integer?. So can you clarify what you actually want, and why predicates like generic-set? aren't enough?Tyrolienne
Thank you. That's good enough. I just feel curious to see if it can return the data type like other languages like Python, Haskell.Pulsifer
Python & Haskell are completely different in this regard; Haskell has a static type system, Python does not. It's probably not a good idea to use the same word ("type") for both of these. I'm relatively confident that you cannot write a function in Haskell that accepts an arbitrary value and returns its type; among other things, I claim this would absolutely destroy parametricity.Toehold
In Haskell: typeOf :: ∀ a. (Typeable a) => a -> TypeRep, though you're right that Typeable 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 -> TypeRepTyrolienne
S
6

#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.

Springspringboard answered 15/11, 2018 at 16:0 Comment(4)
How about the case where i want to check the type for development? For example i get this error: "cadr: contract violation expected: (cons/c any/c pair?) given: 'A2" Now i think 'A2 can be checked with "symbol?" But what if i didn't know that?Waterhouse
"Given some installed library, what you actually care about is, does it provide certain functions and behavior" Actually not just that. You also care about the API of that feature. So, if the function name is changed, there will be a compiler errorLaunch
@Akangka 1. I wrote "and behavior". 2. You're doing a drive-by attempted nit-pick of an 18 month old answer. Instead, if you feel it doesn't address the question well, why not write and submit your own answer.Springspringboard
This covers most use cases, but one use case that I run into: I'm getting a contract violation and the value being printed looks like it should satisfy a predicate but the contract is saying it doesn't. At this point I don't know what predicates to check to figure out what is happening. Knowing ahead of time which predicates to check your value with isn't always a given (at least not for me :D)Isoleucine

© 2022 - 2024 — McMap. All rights reserved.