-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.lisp
36 lines (27 loc) · 1.06 KB
/
number.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
;;;; number.lisp
(in-package #:convertify)
(defgeneric numberify (arg)
(:documentation
"Turn LIST CHARACTER STRING SYMBOL NUMBER OR ARRAY into a NUMBER.
NUMBER returns itself.
CHARACTER returns digit.
STRING returns the number.
SYMBOL returns the number.
ARRAY/LIST returns the number with digits going from left to right, it is recursive and will return the number which is made up of the leaves of the SEQUENCE. (numberify (1 (2 (3)))) => 123
All input must contain digits between 0-9 only, or it will error."))
(defmethod numberify ((num number))
num)
(defmethod numberify ((char character))
(numberify (string char)))
(defmethod numberify ((str string))
;; parse-integer prints the number of digits, not sure how to stop
;; that. Will figure out later and get rid of this let block.
(let ((result
(parse-integer str :junk-allowed nil)))
result))
(defmethod numberify ((lst list))
(numberify (stringify lst)))
(defmethod numberify ((arr array))
(numberify (stringify arr)))
(defmethod numberify ((sym symbol))
(numberify (format nil "~A" sym)))