I want to print just the characters from a LISP list, and I need to verify if an atom is not a number, in CLisp. How can I do this?
How can I verify if an atom is NOT a number in LISP?
nr: #1 dodano: 2017-12-11 00:12
Here’s how to loop through a list and check for things that aren’t numbers:
(loop for thing in list
do (if (numberp thing)
(format nil “I’m a number: ~a~%” thing)
(format nil “Not a number!~%”)
format is analogous to printf in C. And loop is a macro for iteration in Common Lisp. Explaining how that works beside saying “read the keywords as in English and guess what they mean” is beyond the scope of this answer and there are plenty of explanations of loop online, for example the “Loop for black belts” chapter of Peter Seibel’s Practical Common Lisp with is available free online.
Here is how one can write only the characters in a list. For the sake of variety this one explicitly defines a function and uses higher order list manipulation functions instead of explicit loops. Thus it is probably slower.
remove-if-not returns a new list with the same contents as the input except it only includes the elements for which the predicate (in this case characterp) returns true. characterp returns true if and only if its input is a character.
mapc applies a function to each element of a list, discarding the return value. In this case write-char which when called with one argument writes a character to *standard-output*.
nr: #2 dodano: 2017-12-12 08:12
I'm assuming you're asking: "given a list of items, how do I filter out only the numbers in the list?"