>>") (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."> >>") (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.">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMMON LISP.

Similar presentations


Presentation on theme: "COMMON LISP."— 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 "< >" ’( )) 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 ( ). 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 '( ) #’>) ( )

13 Exercises Suppose the function pos+ takes a list and returns a list of each element plus its position: > (pos+ ' ( ) ) ( ) Define this function using (a) recursion, (b) iteration, (c) mapcar.


Download ppt "COMMON LISP."

Similar presentations


Ads by Google