如何在Scheme中获取值的类型? [英] How do I get the type of a value in Scheme?

查看:176
本文介绍了如何在Scheme中获取值的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个在运行时获取值类型的函数。使用示例:

 (get-type a)

其中 a 已经定义 d是一些任意的方案值。



我该怎么做?或者我必须自己实现,使用一个 boolean?号码等等的cond栈? p>

解决方案

在使用Tiny-CLOS类对象系统的Scheme实现中,您只需使用 class 。这是Racket中的一个示例会话,使用Swindle:

  $ racket -I swindle 
欢迎来到Racket v5.2.1。
- > (class-of 42)
#< primitive-class:exact-integer>
- > (class-of #t)
#< primitive-class:boolean>
- > (class-of'foo)
#< primitive-class:symbol>
- > (class-ofbar)
#< primitive-class:immutable-string>

与Guile使用GOOPS类似:

 方案@(诡计用户)> ,use(oop goops)
scheme @(guile-user)> (class-of 42)
$ 1 =#<< class> <整数> 14d6a50>
scheme @(guile-user)> (class-of #t)
$ 2 =#<< class> <布尔> 14c0000>
scheme @(guile-user)> (class-of'foo)
$ 3 =#<< class> <符号GT; 14d3a50>
scheme @(guile-user)> (class-ofbar)
$ 4 =#<< class> <串GT; 14d3b40>


I want a function that gets the type of a value at runtime. Example use:

(get-type a)

where a has been defined to be some arbitrary Scheme value.

How do I do this? Or do I have to implement this myself, using a cond stack of boolean?, number? etc. ?

解决方案

In Scheme implementations with a Tiny-CLOS-like object system, you can just use class-of. Here's a sample session in Racket, using Swindle:

$ racket -I swindle
Welcome to Racket v5.2.1.
-> (class-of 42)
#<primitive-class:exact-integer>
-> (class-of #t)
#<primitive-class:boolean>
-> (class-of 'foo)
#<primitive-class:symbol>
-> (class-of "bar")
#<primitive-class:immutable-string>

And similarly with Guile using GOOPS:

scheme@(guile-user)> ,use (oop goops)
scheme@(guile-user)> (class-of 42)
$1 = #<<class> <integer> 14d6a50>
scheme@(guile-user)> (class-of #t)
$2 = #<<class> <boolean> 14c0000>
scheme@(guile-user)> (class-of 'foo)
$3 = #<<class> <symbol> 14d3a50>
scheme@(guile-user)> (class-of "bar")
$4 = #<<class> <string> 14d3b40>

这篇关于如何在Scheme中获取值的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆