Sei sulla pagina 1di 6

;; Triple the value of a number (defun triple (X) (* 3 X)) (defun double (x) "Multiple X by 2.

" (* 2 x))

We could compute 24 by applying the double transformation 4 times on 1:


> (double (double (double (double 1)))) 16 ;; Negate the sign of a number (defun negate (X) (- X))

Shorthand
(1+ x) (1- x)

Meaning
(+ x 1) (- x 1)

(defun factorial (n) (if (= n 1) 1 (* n (factorial (- n 1))))) (def x 3) (def y 7) (defun f (x) (+ x y)) (defun g (y) (* x y)) (defun h (x y) (+ (f y) (g x))) ;here are the intermediate results of the last part of the assignment ;(h 4 13) = (f 13) + (g 4) = (+ 13 7) + (* 3 4) = 20 + 12 = 32 ;(h (f 2) (g 6)) = (h (+ 2 7) (* 3 6)) = (h 9 18) ;(h 9 18) = (f 18) + (g 9) = (+ 25 27) = 52

The LISP built-in function list-length counts the number of elements in a list. For example,
> (list-length '(2 3 5 7 11 13 17 19))

Let us try to see how such a function can be implemented recursively. A given list L is created by either one of the two constructors, namely nil or a cons:

Case 1: L is nil. The length of an empty list is zero. Case 2: L is constructed by cons. Then L is composed of two parts, namely, (first L) and (rest L). In such case, the length of L can be obtained inductively by adding 1 to the length of (rest L).

Formally, we could implement our own version of list-length as follows:


(defun recursive-list-length (L) "A recursive implementation of list-length." (if (null L) 0 (1+ (recursive-list-length (rest L))))) Here, we use the recognizer null to differentiate how L is constructed. In case L is nil, we return 0 as its length. Otherwise, L is a cons, and we return 1 plus the length of (rest L). Recall that (1+ n) is simply a shorthand for (+ n 1).

LISP defines a function (nth N L) that returns the N'th member of list L (assuming that the elements are numbered from zero onwards):
> (nth 0 '(a b c d)) A > (nth 2 '(a b c d)) C

We could implement our own version of nth by linear recursion. Given N and L, either L is nil or it is constructed by cons.

Case 1: L is nil. Accessing the N'th element is an undefined operation, and our implementation should arbitrarily return nil to indicate this. Case 2: L is constructed by a cons. Then L has two components: (first L) and (rest L). There are two subcases: either N = 0 or N > 0: o Case 2.1: N = 0. The zeroth element of L is simply (first L). o Case 2.2: N > 0. The N'th member of L is exactly the (N-1)'th member of (rest L).

The following code implements our algorithm:

(defun list-nth (N L) "Return the N'th member of a list L." (if (null L) nil

(if (zerop N) (first L) (list-nth (1- N) (rest L))))) Recall that (1- N) is merely a shorthand for (- N 1).

Notice that we have a standard if-then-else-if structure in our implementation of list-nth. Such logic can alternatively be implemented using the cond special form.
(defun list-nth (n L) "Return the n'th member of a list L." (cond ((null L) nil) ((zerop n) (first L)) (t (list-nth (1- n) (rest L))))) The cond form above is evaluated as follows. The condition (null L)

is evaluated first. If the result is true, then nil is returned. Otherwise, the condition (zerop n) is evaluated. If the condition holds, then the value of (first L) is returned. In case neither of the conditions holds, the value of (list-nth (1- n) (rest L)) is returned LISP defines a function (member E L) that returns non-NIL if E is a member of L.
> (member 'b '(perhaps today is a good day to die)) ; test fails NIL > (member 'a '(perhaps today is a good day to die)) ; returns non-NIL '(a good day to die)

We implement our own recursive version as follows:

(defun list-member (E L) "Test if E is a member of L." (cond ((null L) nil) ((eq E (first L)) t) (t (list-member E (rest L)))))

The correctness of the above implementation is easy to justify. The list L is either constructed by nil or by a call to cons:

Case 1: L is nil. L is empty, and there is no way E is in L. Case 2: L is constructed by cons Then it has two components: (first L) and (rest L). There are two cases, either (first L) is E itself, or it is not. o Case 2.1: E equals (first L). This means that E is a member of L, o Case 2.2: E does not equal (first L). Then E is a member of L iff E is a member of (rest L).

(= x y)

True if x and y evaluate to the same number.

(eq x y) (eql x y) (equal x y)

True if x and y evaluate to the same symbol. True if x and y are either = or eq. True if x and y are eql or if they evaluate to the same list.

LISP defines a function append that appends one list by another:


> (append '(a b c) '(c d e)) (A B C C D E)

We implement a recursive version of append. Suppose we are given two lists L1 and L2. L1 is either nil or constructed by cons.

Case 1: L1 is nil. Appending L2 to L1 simply results in L2. Case 2: L1 is composed of two parts: (first L1) and (rest L1). If we know the result of appending L2 to (rest L1), then we can take this result, insert (first L1) to the front, and we then have the list we want.

Formally, we define the following function:


(defun list-append (L1 L2) "Append L1 by L2." (if (null L1) L2 (cons (first L1) (list-append (rest L1) L2))))

Let us begin by writing a function that returns an even element in a list of numbers:
(defun find-even (L) "Given a list L of numbers, return the leftmost even member." (if (null L) nil (if (evenp (first L)) (first L) (find-even (rest L)))))

BT:
1. Vit hm tm phn t ln nht trong mt danh sch mt chiu. (getMax ( 3 2 6 5 4 3 6 7) ) => 7 2. Vit hm depth tnh su ca mt danh sch: (depth (a b)) => 1 (depth (a (b (c)))) => 3

3. Ta c chng trnh LISP tnh tch ly tha (luy_thua x n) vi COND v n>0 nh sau (defun luy_thua (x n) (cond ((= n 0) 1) ((> n 0) (* x (luy_thua x (1- n)) ) ) Hy vit li chng trnh trn v thay th COND bng IF 4. Vit chng trnh tnh dy Fibonacci sau bng ngn ng LISP 0, Fn := F(n) := 1, F(n-1) + F(n-2), khi n=0 khi n=1 khi n>1

5. Vit chng trnh gii phng trnh bc 1: ax+b=0

N THI:
6. Cho chng trnh sau y void main() { int value = 2, list [5] = {1, 3, 5, 7, 9}; swap(value, list[0]); swap(list[0], list[1]); swap(value, list[value]); }

void swap(int a, int b){ int temp; temp = a; a=b;

b=temp; } i vi tng cch truyn tham s, lit k gi tr ca value v list sau ba ln gi hm swap a/ Truyn tham tr b/ Truyn quy chiu c/ Truyn tham tr-kt qu 7. S khc nhau gia m hnh lp trnh hm v m hnh lp trnh cu trc l g?

Potrebbero piacerti anche