How to check type in Racket?
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.
types racket
|
show 1 more comment
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.
types racket
generic-set?. As in(generic-set? 1-9) = #true
– Alex Knauth
Nov 14 '18 at 2:06
So you have to know the type before? What if I have no idea about the type of the output?
– user8314628
Nov 14 '18 at 2:07
1
If you want to see if its a set, usegeneric-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 whatxis.5has at least three "types"any/c,number?, andinteger?. So can you clarify what you actually want, and why predicates likegeneric-set?aren't enough?
– Alex Knauth
Nov 14 '18 at 2:12
1
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.
– John Clements
Nov 14 '18 at 5:46
1
In Haskell:typeOf :: ∀ a. (Typeable a) => a -> TypeRep, though you're right thatTypeable adoes 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
– Alex Knauth
Nov 14 '18 at 14:22
|
show 1 more comment
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.
types racket
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.
types racket
types racket
asked Nov 14 '18 at 1:57
user8314628user8314628
56529
56529
generic-set?. As in(generic-set? 1-9) = #true
– Alex Knauth
Nov 14 '18 at 2:06
So you have to know the type before? What if I have no idea about the type of the output?
– user8314628
Nov 14 '18 at 2:07
1
If you want to see if its a set, usegeneric-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 whatxis.5has at least three "types"any/c,number?, andinteger?. So can you clarify what you actually want, and why predicates likegeneric-set?aren't enough?
– Alex Knauth
Nov 14 '18 at 2:12
1
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.
– John Clements
Nov 14 '18 at 5:46
1
In Haskell:typeOf :: ∀ a. (Typeable a) => a -> TypeRep, though you're right thatTypeable adoes 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
– Alex Knauth
Nov 14 '18 at 14:22
|
show 1 more comment
generic-set?. As in(generic-set? 1-9) = #true
– Alex Knauth
Nov 14 '18 at 2:06
So you have to know the type before? What if I have no idea about the type of the output?
– user8314628
Nov 14 '18 at 2:07
1
If you want to see if its a set, usegeneric-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 whatxis.5has at least three "types"any/c,number?, andinteger?. So can you clarify what you actually want, and why predicates likegeneric-set?aren't enough?
– Alex Knauth
Nov 14 '18 at 2:12
1
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.
– John Clements
Nov 14 '18 at 5:46
1
In Haskell:typeOf :: ∀ a. (Typeable a) => a -> TypeRep, though you're right thatTypeable adoes 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
– Alex Knauth
Nov 14 '18 at 14:22
generic-set?. As in (generic-set? 1-9) = #true– Alex Knauth
Nov 14 '18 at 2:06
generic-set?. As in (generic-set? 1-9) = #true– Alex Knauth
Nov 14 '18 at 2:06
So you have to know the type before? What if I have no idea about the type of the output?
– user8314628
Nov 14 '18 at 2:07
So you have to know the type before? What if I have no idea about the type of the output?
– user8314628
Nov 14 '18 at 2:07
1
1
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?– Alex Knauth
Nov 14 '18 at 2:12
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?– Alex Knauth
Nov 14 '18 at 2:12
1
1
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.
– John Clements
Nov 14 '18 at 5:46
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.
– John Clements
Nov 14 '18 at 5:46
1
1
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 -> TypeRep– Alex Knauth
Nov 14 '18 at 14:22
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 -> TypeRep– Alex Knauth
Nov 14 '18 at 14:22
|
show 1 more comment
1 Answer
1
active
oldest
votes
#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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292108%2fhow-to-check-type-in-racket%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
#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.
add a comment |
#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.
add a comment |
#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.
#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.
answered Nov 15 '18 at 16:00
Greg HendershottGreg Hendershott
10.5k42343
10.5k42343
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292108%2fhow-to-check-type-in-racket%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
generic-set?. As in(generic-set? 1-9) = #true– Alex Knauth
Nov 14 '18 at 2:06
So you have to know the type before? What if I have no idea about the type of the output?
– user8314628
Nov 14 '18 at 2:07
1
If you want to see if its a set, use
generic-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 whatxis.5has at least three "types"any/c,number?, andinteger?. So can you clarify what you actually want, and why predicates likegeneric-set?aren't enough?– Alex Knauth
Nov 14 '18 at 2:12
1
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.
– John Clements
Nov 14 '18 at 5:46
1
In Haskell:
typeOf :: ∀ a. (Typeable a) => a -> TypeRep, though you're right thatTypeable adoes 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– Alex Knauth
Nov 14 '18 at 14:22