1 / 13

COMMON LISP

COMMON LISP. EXERCISES. As you write each of the following functions, test it by calling it from top level with appropriate inputs before proceeding on to the next function.

rowena
Download Presentation

COMMON LISP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMMON LISP

  2. EXERCISES • As you write each of the following functions, test it by calling it from top level with appropriate inputs before proceeding on to the next function. a. Write a recursive function SPACE-OVER that takes a number N as input and moves the cursor to the right by printing N spaces, one at a time. SPACE should print ‘‘Error!’’ if N is negative. Test it by using the function TEST. Try (TEST 5) and (TEST -5). (defun test (n) (format t "~%>>>") (space-over n) (format t "<<<")) b. Write a function PLOT-ONE-POINT that takes two inputs, PLOTTING-STRING and Y-VAL, prints PLOTTING-STRING (without the quotes) in column Y-VAL, and then moves to a new line. The leftmost column is numbered zero.

  3. EXERCISES c. Write a function PLOT-POINTS that takes a string and a list of y values as input and plots them. (PLOT-POINTS "< >" ’(4 6 8 10 8 6 4)) should print < > < > < > < > < > < > < > d. Write a function GENERATE that takes two numbers M and N as input and returns a list of the integers from M to N. (GENERATE -3 3) should return (-3 -2 -1 0 1 2 3). e. Write the MAKE-GRAPH function. MAKE-GRAPH should prompt for the values of FUNC, START, END, and PLOTTINGSTRING, and then graph the function. Note: You can pass FUNC as an input to MAPCAR to generate the list of y values for the function. What will the second input to MAPCAR be? f. Define the SQUARE function and graph it over the range -7 to 7. Use your first name as the plotting symbol.

  4. EXERCISES • In this exercise we will write a program for producing a graph of an arbitrary function. The program will prompt for a function name F and then plot y = F(x) for a specified range of x values. Here is an example of how the program works:

  5. DNA AND RNA • DNA, and the related molecule RNA, make up the genetic material found in viruses and every type of cell, from bacteria (細菌) to people. • A strand of DNA is very much like a chain of cons cells; the elements of the chain are of four types, corresponding to the four bases adenine (腺嘌呤A), thymine (胸腺嘧啶T), guanine (鳥嘌呤G), and cytosine (胞嘧啶C). • We will represent a strand of DNA by a list of bases. • The list (A G G T C A T T G) corresponds to a strand that is nine bases long; the first base being adenine and the next two guanine. • Here is a schematic diagram of the strand: --------------------------------------------- ! ! ! ! ! ! ! ! ! A G G T C A T T G

  6. DNA AND RNA • Each of the four bases has a complement with which it can form a pair. • Adenine(腺嘌呤) pairs with thymine(胸腺嘧啶), while guanine(鳥嘌呤) pairs with cytosine(胞嘧啶). • Two single strands of DNA can combine to form double-stranded DNA when each of their corresponding bases are complementary. • The strand (A G G T C A T T G) and the strand (T C C A G T A A C) are complementary, for example. • Double-stranded DNA looks like this: --------------------------------------------- ! ! ! ! ! ! ! ! ! A G G T C A T T G . . . . . . . . . . . . . . . . . . T C C A G T A A C ! ! ! ! ! ! ! ! ! ---------------------------------------------

  7. EXERCISES • Write iterative solutions to all parts of this exercise that require repetitive actions. a. Write a function COMPLEMENT-BASE that takes a base as input and returns the matching complementary base. (COMPLEMENTBASE ’A) should return T; (COMPLEMENT-BASE ’T) should return A; and so on. b. Write a function COMPLEMENT-STRAND that returns the complementary strand of a sequence of single-stranded DNA. (COMPLEMENT-STRAND ’(A G G T)) should return (T C C A). c. Write a function MAKE-DOUBLE that takes a single strand of DNA as input and returns a double-stranded version. We will represent double-stranded DNA by making a list of each base and its complement. (MAKE-DOUBLE ’(G G A C T)) should return ((G C) (G C) (A T) (C G) (T A)).

  8. EXERCISES d. One of the important clues to DNA’s double-stranded nature was the observation that in naturally occurring DNA, whether from people, animals, or plants, the observed percentage of adenine is always very close to that of thymine, while the observed percentage of guanine is very close to that of cytosine. • Write a function COUNTBASES that counts the number of bases of each type in a DNA strand, and returns the result as a table. • Your function should work for both single- and double-stranded DNA. • Example: • (COUNTBASES ’((G C) (A T) (T A) (T A) (C G))) should return ((A 3) (T 3) (G 2) (C 2)), whereas (COUNTBASES ’(A G T A C T C T)) should return ((A 2) (T 3) (G 1) (C 2)). • In the latter case the percentages are not equal because we are working with only a single strand. What answer do you get if you apply COUNT-BASES to the corresponding double-stranded sequence?

  9. EXERCISES e. Write a predicate PREFIXP that returns T if one strand of DNA is a prefix of another. To be a prefix, the elements of the first strand must exactly match the corresponding elements of the second, which may be longer. Example: (G T C) is a prefix of (G T C A T), but not of (A G G T C). f. Write a predicate APPEARSP that returns T if one DNA strand appears anywhere within another. For example, (C A T) appears in (T C A T G) but not in (T C C G T A). Hint: If x appears in y, then x is a either a prefix of y, or of (REST y), or of (REST (REST y)), and so on. g. Write a predicate COVERP that returns T if its first input, repeated some number of times, matches all of its second input. Example: (A G C) covers (A G C A G C A G C) but not (A G C T T G). You may assume that neither strand will be NIL.

  10. EXERCISES h. Write a function PREFIX that returns the leftmost N bases of a DNA strand. (PREFIX 4 ’(C G A T T A G)) should return (C G A T). Do not confuse the function PREFIX with the predicate PREFIXP. i. Biologists have found that portions of some naturally occurring DNA strands consist of many repetitions of a short ‘‘kernel’’ sequence. Write a function KERNEL that returns the shortest prefix of a DNA strand that can be repeated to cover the strand. (KERNEL ’(A G C A G C A G C)) should return (A G C). (KERNEL ’(A A A A A)) should return (A). (KERNEL ’(A G G T C)) should return (A G G T C), because in this case only a single repetition of the entire strand will cover the strand. Hint: To find the kernel, look at prefixes of increasing length until you find one that can be repeated to cover the strand.

  11. EXERCISES • A friend is trying to write a function that returns the sum of all the non-nil elements in a list. He has written two versions of this function, and neither of them work. Explain what's wrong with each, and give a correct version: (a) (defun summit (1st) (remove nil 1st) (apply #'+ 1st)) (b) (defun summit (1st) (let ((x (car 1st))) (if (null x) (summit (cdr 1st)) (+ x (summit (cdr 1st))))))

  12. EXERCISES • Give iterative and recursive definitions of a function that (a) takes a positive integer and prints that many dots. (b) takes a list and returns the number of times the symbol a occurs in it. • Define a function that takes a list and returns a list indicating the number of times each (eql) element appears, sorted from most common element to least common: > (occurrences '( a b a d a c d e a )) ((A . 4) (C . 2) (D . 2) (B . 1)) or ((A 4) (C 2) (D 2) (B 1)) PS: > (sort '(0 2 1 3 8 ) #’>) ( 8 3 2 1 0 )

  13. EXERCISES • Suppose the function pos+ takes a list and returns a list of each element plus its position: > (pos+ ' ( 7 5 1 4 ) ) (7 6 3 7) Define this function using (a) recursion, (b) iteration, (c) mapcar.

More Related