Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check type in Racket?

Tags:

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.

like image 957
user8314628 Avatar asked Nov 14 '18 01:11

user8314628


People also ask

Does Racket have types?

The most basic types in Typed Racket are those for primitive data, such as True and False for booleans, String for strings, and Char for characters. Each symbol is given a unique type containing only that symbol. The Symbol type includes all symbols.

Are typed rackets faster?

Typed Racket provides a type-driven optimizer that rewrites well-typed programs to potentially make them faster. It should in no way make your programs slower or unsafe. Thus the type hinting can make your programs faster, but it guarantees the programs wont be slower than #lang racket as well.

What is a predicate in racket?

A predicate is what we call functions often called in predicate position in conditionals (like if and cond ).


1 Answers

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

like image 67
Greg Hendershott Avatar answered Oct 20 '22 20:10

Greg Hendershott