Find the nth fibonacci number (defun fib (n) Find an item on a tree (defun find-tree (tree word) (interleave (rest one) (rest two)))))) Repeating Operations More about Recursion 1. ![]() interleave two lists (defun interleave (one two) (cons (first lst) (compress (rest lst)))))) 7. Eliminate consecutive duplicates in a list (defun compress (lst) (append (list (first lst) (first lst)) (dupli-list (rest lst))))) 6. Duplicate each element in a list (defun dupli-list (lst) Find the maximum element of a list (defun max-list (lst) Find whether each number in a list is even or odd 4. Reverse each string in a list of strings 3. ![]() Count the number of elements in a list 2. (concatenate 'string (subseq word 1) (subseq word 0 1) "ay")))) Creating Dialogue Boxes Writing Programs Processing Items in a List 1. (string= initial "i") (string= initial "o") (or (string= initial "a") (string= initial "e") Write a piglatin translator (defun piglatin (word) Test whether an object is a list of two numbers 3. (concatenate 'string (subseq str places) (subseq str 0 places))) Printing Testing a Result 1. Rotate a string n places to the left (defun rotate (str places) Or without using length: (defun midverse (word) (reverse (subseq word 1 (- (length word) 1))) Reverse the middle letters of a word (defun midverse (word) Return the last item in a list (defun last-elt (lst) Return a random item from a list (defun random-elt (lst) Duplicate the first item in a list (defun dup (lst) (cons (second lst) (cons (first lst) (rest (rest lst))))) 2. Swap the first two items in a list (defun swap (lst) Substitute values into a quadratic equation (defun pseudo-primes (x) Cube the sum of two numbers (defun cubesum (a b)Īnswer)) 4. Find the average of three numbers (defun average3 (number1 number2 number3) Convert between km and miles (defparameter kilometresinmiles 0.621371192) Find the result of throwing two dice (defun two-dice () Find the nth triangular number (defun triangular (n) (list 1 (list 2 (list 3 4))) Expressions Defining Procedures 1. Using format (as in (format nil "~D" n)) is safer (you no longer need to care about things like *print-base* and *print-radix*) but it is still vulnerable to the ambient value of *print-pretty* and hence to the above games with the pprint dispatch table.Not all the exercises have answers yet. ) if you want to be sure that the output you get is the output you expect. This may be the greatest monster which lurks, but there are other, lesser ones: *print-base* *print-radix* among them.Īt the very least you should surround calls to these functions with (with-standard-io-syntax. The way you get this is by having *print-pretty* set to true and then doing a couple of entertaining things to the pprint dispatch table. This is entirely legal output and any conforming implementation would produce it (or in fact something similar as there were some random numbers involved). I think it's worth pointing out that you need to be extremely cautious about the slavering monsters which lurk around the corners when using any or all of write-to-string, prin1-to-string, and princ-to-string.Īs an example consider this: > (dotimes (i 20)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |