入力された数を文字列として扱う
プログラムの中で入力を受け付けたいということはよくある。この入力自体は (read) で受け取ることができる。しかし、入力が数字のみの場合、そのまま文字列として扱うことはできない。
? (progn (format t "Type number:") (setf x (read)) (format t (concatenate 'string "Typed number is " x))) Type number:123 > Error: The value 123 is not of the expected type SEQUENCE. > While executing: CCL::SEQUENCE-TYPE, in process listener(1). > Type :POP to abort, :R for a list of available restarts. > Type :? for other options.
こういう場合は、(write-to-string (read)) というように書くことで、数字ではなく文字列として扱うことができる。まあ、文字列を数値として扱えるようにするための関数、といったところか。
(progn (format t "Type number:") (setf x (write-to-string (read))) (format t (concatenate 'string "Typed number is " x))) Type number:123 Typed number is 123 NIL