Lisp in Small Parts Introduction Debugging in LispWorks Getting Started Lists Expressions Defining Procedures Variables Manipulating Lists Strings Printing Testing a Result Creating Dialogue Boxes Writing Programs Processing Items in a List Repeating Operations More about Recursion Generalising Procedures Projects Animals Anagrams Recipes Map Turtle Graphics Logic Mazes Number Countdown Answers to Exercises Index of Procedures Suggested sites Answers to Exercises Not all the exercises have answers yet. Lists 1. (list 1 (list 2 (list 3 4))) Expressions Defining Procedures 1. Square a number (defun square (number) (* number number)) 2. Find the nth triangular number (defun triangular (n) (/ (* n (+ n 1)) 2)) 3. Find the result of throwing two dice (defun two-dice () (+ (+ 1 (random 6)) (+ 1 (random 6)))) Variables 1. Convert between km and miles (defparameter kilometresinmiles 0.621371192) (defun convert-km (km) (* km kilometresinmiles)) (defun convert-miles (miles) (/ miles kilometresinmiles)) 2. Find the average of three numbers (defun average3 (number1 number2 number3) (/ (+ number1 number2 number3) 3)) 3. Cube the sum of two numbers (defun cubesum (a b) (let* ((total (+ a b)) (answer (* total total total))) answer)) 4. Substitute values into a quadratic equation (defun pseudo-primes (x) (+ (- (* x x) x) 41)) Manipulating Lists 1. Swap the first two items in a list (defun swap (lst) (cons (second lst) (cons (first lst) (rest (rest lst))))) 2. Duplicate the first item in a list (defun dup (lst) (cons (first lst) lst)) 3. Return a random item from a list (defun random-elt (lst) (nth (random (length lst)) lst)) 4. Return the last item in a list (defun last-elt (lst) (nth (- (length lst) 1) lst)) Strings