Write a function called square-odd-p. This function takes a single number as input, it squares this number and tests to see if the square of the number is odd.
(defun square-odd-p (x)
(oddp (* x x)))
Creating Lists in Lisp
cons is a Common Lisp primitive, that creates lists from an atom
(number or letter) and a list:
(cons 1 '(2 3))
(1 2 3)
We'll use cons to help us write a function to replace the first member of a list. In order to do this we'll:
- Have to take two inputs to our function: the new item and the list
- We'll use the rest function to return everything but the first member of the list
- We can then cons our new item to the start of this list:
(cons new-item (rest the-list)))
And an example:
(replace-first 'peter-pan '(one flew over the cuckoos nest))
(PETER-PAN FLEW OVER THE CUCKOOS NEST)
In the next post we'll look at some more examples of building and manipulating lists.