本文介绍了检查参数是列表还是原子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查某物是否是原子?我正在寻找诸如 number?
或 list?
之类的东西.
How do I check if something is an atom? I'm looking for something like number?
or list?
.
推荐答案
通常,您也希望排除空列表:
Usually, you'll want to exclude the empty list too:
(define (atom? x) (not (or (pair? x) (null? x))))
或者,如果你想变得更加迂腐,那么也禁止使用向量:
or, if you want to be more pedantic, then forbid vectors too:
(define (atom? x) (not (or (pair? x) (null? x) (vector? x))))
当然你可以在这里添加更多——因为它被标记为一个球拍问题,你可能想要添加哈希表、结构等.所以它也可以更容易地指定值的种类你确实认为是原子:
And of course you can add much more here -- since it's marked as a racket question, you might want to add hash tables, structs, etc etc. So it can just as well be easier to specify the kinds of values that you do consider as atoms:
(define (atom? x)
(ormap (lambda (p) (p x)) (list number? symbol? boolean? string?)))
或使用球拍合约系统:
(define atom? (or/c number? symbol? boolean? string?))
这篇关于检查参数是列表还是原子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!