Skip to main content
2 of 4
Correcting a bug.
gigiair
  • 2.3k
  • 1
  • 11
  • 15

the read function can be applied to a string and then the numberp function tests whether the read object represents a number or not, integer, or float.

(mapcar (lambda(x)(numberp(read x))) (list "123" "1.23" "sqrt(123)" "12e-3" "12ab3")) 

=>

 (t t nil t nil) 

You catch any error with this function

(defun string-numberp(s) (condition-case invalid (numberp(read s)) (error nil))) 

Test :

(mapcar #'string-numberp (list "12345" "12.345" "#12345" "." "," "\n")) 

=>

(t t nil nil nil nil) 
gigiair
  • 2.3k
  • 1
  • 11
  • 15