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

1. Reverse the middle letters of a word

(defun midverse (word)
  (concatenate 'string 
               (subseq word 0 1) 
               (reverse (subseq word 1 (- (length word) 1))) 
               (subseq word (- (length word) 1))))

or without using length:

(defun midverse (word)
  (concatenate 'string 
               (subseq word 0 1) 
               (subseq (reverse (subseq word 1)) 1) 
               (subseq (reverse word) 0 1)))

2. Rotate a string n places to the left

(defun rotate (str places)
  (concatenate 'string (subseq str places) (subseq str 0 places)))

Printing

Testing a Result

1. Test whether a string is a palindrome

2. Test whether an object is a list of two numbers

3. Write a piglatin translator

(defun piglatin (word)
 (let* ((initial (subseq word 0 1)))
   (if
     (or (string= initial "a") (string= initial "e") 
         (string= initial "i") (string= initial "o")
         (string= initial "u"))
     (concatenate 'string word "way")
     (concatenate 'string (subseq word 1) (subseq word 0 1) "ay"))))

Creating Dialogue Boxes

Writing Programs

Processing Items in a List

1. Count the number of elements in a list

2. Reverse each string in a list of strings

3. Find whether each number in a list is even or odd

4. Find the maximum element of a list

(defun max-list (lst)
  (if (null (rest lst)) 
      (first lst)
    (let* ((maxrest (max-list (rest lst))))
      (if (> (first lst) maxrest)
          (first lst) maxrest))))

5. Duplicate each element in a list

(defun dupli-list (lst)
  (if (null lst) nil 
    (append (list (first lst) (first lst)) (dupli-list (rest lst)))))

6. Eliminate consecutive duplicates in a list

(defun compress (lst)
  (if (null lst) nil 
    (if (eq (first lst) (first (rest lst)))
        (compress (rest lst))
      (cons (first lst) (compress (rest lst))))))

7. interleave two lists

(defun interleave (one two)
  (if (null one) nil
    (cons (first one)
          (cons (first two)
                (interleave (rest one) (rest two))))))

Repeating Operations

More about Recursion

1. Count the items on a tree

2. Find an item on a tree

(defun find-tree (tree word)
  (if (null tree) nil
    (if (string= (first tree) word) t
      (if (find-tree (second tree) word) t
        (find-tree (third tree) word)))))

3. Find the nth fibonacci number

(defun fib (n)
  (if (< n 3) 1
    (+ (fib (- n 1)) (fib (- n 2)))))

4. Find a specified number on Pascal's triangle

(defun pascal (n r)
  (if (= n 1) 1
    (if (= n r) 1
      (+ (pascal (- n 1) (- r 1)) (pascal n (- r 1))))))

Generalising Procedures

1. Repeat a procedure for a range of numbers

(defun repeat-for (from to function)
  (if (> from to) nil
    (progn
      (funcall function from)
      (repeat-for (+ from 1) to function))))

2. Combine a list of numbers using a binary operator

(defun combine (function lst)
  (if (null (third lst))
      (funcall function (first lst) (second lst))
    (funcall function (first lst) (combine function (rest lst)))))

or:

(defun combine (function lst)
  (funcall function (first lst) 
           (if (null (third lst))
               (second lst)
             (combine function (rest lst)))))

blog comments powered by Disqus