Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

scheme - Check if an argument is a list or an atom

How do I check if something is an atom? I'm looking for something like number? or list?.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?)))

or using the racket contract system:

(define atom? (or/c number? symbol? boolean? string?))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...