You are on page 1of 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))

(1+ x) (1- x)

(+ 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))

Then L is composed of two parts. The length of an empty list is zero. The N'th member of L is exactly the (N-1)'th member of (rest L). Accessing the N'th element is an undefined operation. Given N and L. o Case 2. There are two subcases: either N = 0 or N > 0: o Case 2. Case 2: L is constructed by a cons.8 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. either L is nil or it is constructed by cons. Case 2: L is constructed by cons. and our implementation should arbitrarily return nil to indicate this. (first L) and (rest L). namely nil or a cons: • • Case 1: L is nil. and we return 1 plus the length of (rest L). namely. Recall that (1+ n) is simply a shorthand for (+ n 1). In such case." (if (null L) 0 (1+ (recursive-list-length (rest L))))) Here. The zeroth element of L is simply (first L).1: N = 0. we return 0 as its length.2: N > 0. • • Case 1: L is nil. Otherwise. the length of L can be obtained inductively by adding 1 to the length of (rest L). Then L has two components: (first L) and (rest L). The following code implements our algorithm: (defun list-nth (N L) "Return the N'th member of a list L. we could implement our own version of list-length as follows: (defun recursive-list-length (L) "A recursive implementation of list-length. Formally. L is a cons. In case L is nil. 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. we use the recognizer null to differentiate how L is constructed." (if (null L) nil .

test fails NIL > (member 'a '(perhaps today is a good day to die)) . Case 2: L is constructed by cons Then it has two components: (first L) and (rest L). the value of (list-nth (1. The list L is either constructed by nil or by a call to cons: • • Case 1: L is nil. the condition (zerop n) is evaluated. . or it is not.(if (zerop N) (first L) (list-nth (1. In case neither of the conditions holds.n) (rest L))))) The cond form above is evaluated as follows.2: E does not equal (first L). 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. > (member 'b '(perhaps today is a good day to die)) ." (cond ((null L) nil) ((zerop n) (first L)) (t (list-nth (1.N) is merely a shorthand for (." (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. then the value of (first L) is returned. There are two cases. L is empty. Notice that we have a standard if-then-else-if structure in our implementation of list-nth. Otherwise. either (first L) is E itself.N 1). The condition (null L) is evaluated first.1: E equals (first L). (= x y) True if x and y evaluate to the same number. (defun list-nth (n L) "Return the n'th member of a list L. Such logic can alternatively be implemented using the cond special form. then nil is returned. This means that E is a member of L.N) (rest L))))) Recall that (1. If the condition holds. o Case 2. If the result is true. Then E is a member of L iff E is a member of (rest L).n) (rest L)) is returned LISP defines a function (member E L) that returns non-NIL if E is a member of L. o Case 2. and there is no way E is in L.

True if x and y are eql or if they evaluate to the same list. (getMax ‘( 3 2 6 5 4 3 6 7) ) => 7 2. Viết hàm tìm phần tử lớn nhất trong một danh sách một chiều.(eq x y) (eql x y) (equal x y) True if x and y evaluate to the same symbol. Case 2: L1 is composed of two parts: (first L1) and (rest L1). Viết hàm depth tính độ sâu của một danh sách: (depth (a b)) => 1 (depth (a (b (c)))) => 3 . return the leftmost even member. Suppose we are given two lists L1 and L2. then we can take this result. Appending L2 to L1 simply results in L2." (if (null L) nil (if (evenp (first L)) (first L) (find-even (rest L))))) BT: 1. L1 is either nil or constructed by cons." (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. insert (first L1) to the front. If we know the result of appending L2 to (rest L1). True if x and y are either = or eq. and we then have the list we want. we define the following function: (defun list-append (L1 L2) "Append L1 by L2. Formally. • • Case 1: L1 is nil. 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.

3. swap(list[0]. list[value]). list[0]). swap(value. swap(value. F(n-1) + F(n-2). Ta có chương trình LISP để tính tích lũy thừa (luy_thua x n) với COND và n>0 như sau (defun luy_thua (x n) (cond ((= n 0) 1) ((> n 0) (* x (luy_thua x (1. } void swap(int a. Fn := F(n) := 1.n)) ) ) Hãy viết lại chương trình trên và thay thế COND bằng IF 4. Viết chương trình giải phương trình bậc 1: ax+b=0 ÔN THI: 6. int b){ int temp. list[1]). . khi n=0 khi n=1 khi n>1 5. 9}. a=b. temp = a. list [5] = {1. Cho chương trình sau đây void main() { int value = 2. 7. 5.3. Viết chương trình tính dãy Fibonacci sau bằng ngôn ngữ LISP 0.

Sự khác nhau giữa mô hình lập trình hàm và mô hình lập trình cấu trúc là gì? .b=temp. liệt kê giá trị của value và list sau ba lần gọi hàm swap a/ Truyền tham trị b/ Truyền quy chiếu c/ Truyền tham trị-kết quả 7. } Đối với từng cách truyền tham số.