[racket-users] datatypes

63 views
Skip to first unread message

Kevin Forchione

unread,
Jul 17, 2019, 12:16:28 AM7/17/19
to Racket-Users List
Hi guys,
Is there any function in Racket that will return a symbol representation of a value’s datatype? We can interrogate them with predicates, but it occurs to me that this information must be carried in the object’s syntax somewhere… otherwise the syntax->datum wouldn’t work. Of course I may be overlooking something :)

Kevin

Alex Knauth

unread,
Jul 17, 2019, 1:36:26 AM7/17/19
to Kevin Forchione, Racket-Users List

On Jul 17, 2019, at 12:16 AM, Kevin Forchione <lys...@gmail.com> wrote:

Hi guys,
Is there any function in Racket that will return a symbol representation of a value’s datatype? We can interrogate them with predicates, but it occurs to me that this information must be carried in the object’s syntax somewhere… otherwise the syntax->datum wouldn’t work. Of course I may be overlooking something :)

I'm not sure what you mean by "carried in the object's syntax" or why syntax->datum is relevant. Could you clarify with a concrete example?

Anyway putting aside the syntax part and only looking at values, the `describe` [1] package, in particular the `variant` [2] function might be helpful to you or not depending on what you mean by "datatype".

> (require describe)
> (variant 5)
'fixnum-integer
> (variant (expt 2 65))
'bignum-integer
> (variant 3.0)
'inexact-number
> (variant 'auu)
'symbol
> (variant "oee")
'string
> (variant (list 1 2 3))
'pair
> (variant (list))
'empty-list
> (variant (vector 1 2 3))
'vector
> (variant (λ (x) x))
'procedure


Kevin

Kevin Forchione

unread,
Jul 17, 2019, 10:50:44 AM7/17/19
to Alex Knauth, Racket-Users List


On Jul 16, 2019, at 10:36 PM, Alex Knauth <alex...@knauth.org> wrote:



On Jul 17, 2019, at 12:16 AM, Kevin Forchione <lys...@gmail.com> wrote:

Hi guys,
Is there any function in Racket that will return a symbol representation of a value’s datatype? We can interrogate them with predicates, but it occurs to me that this information must be carried in the object’s syntax somewhere… otherwise the syntax->datum wouldn’t work. Of course I may be overlooking something :)

I'm not sure what you mean by "carried in the object's syntax" or why syntax->datum is relevant. Could you clarify with a concrete example?

Anyway putting aside the syntax part and only looking at values, the `describe` [1] package, in particular the `variant` [2] function might be helpful to you or not depending on what you mean by "datatype”.

Thanks, Alex! That’s exactly what I’m looking for. My remark on syntax->datum. was simply speculation. A better way to put it is: how does variant able to convert a racket value into a symbol representing its datatype? I was thinking that information must be part of the object’s syntax-object, but it doesn’t look like it. Still, variant reports ‘(a . b) as a ‘pair. and that’s amazing. How is it obtaining that information?

Kevin

Ryan Kramer

unread,
Jul 17, 2019, 11:33:03 AM7/17/19
to Racket Users
 Still, variant reports ‘(a . b) as a ‘pair. and that’s amazing. How is it obtaining that information?

I just looked it up and yeah, it is pretty cool: (struct->vector '(a . b)) is doing most of the work. See the comments and implementation here: https://github.com/mbutterick/describe/blob/b468661a0e76fbe5ef46fe0e89e030f43f7b3acc/describe/main.rkt#L78

Reply all
Reply to author
Forward
0 new messages