ex lisp

Upload: thinh-dang

Post on 06-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Ex LISP

    1/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 Meaning(1+ x) (+ x 1)

    (1- x) (- 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))

  • 8/2/2019 Ex LISP

    2/6

    8

    Let us try to see how such a function can be implemented recursively. A given listL 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.

    ThenL is composed of two parts, namely, (first L) and (rest L). In such case, the

    length ofL can be obtained inductively by adding 1 to the length of(rest L).

    Formally, we could implement our own version oflist-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 recognizernull to differentiate howL is constructed. In caseL is nil, wereturn 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 NL) that returns theN'th member of listL (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 ofnth by linear recursion. GivenNandL, eitherL is nil

    or it is constructed by cons.

    Case 1:L is nil.

    Accessing theN'th element is an undefined operation, and our implementation shouldarbitrarily return nil to indicate this.

    Case 2:L is constructed by a cons.

    ThenL has two components: (first L) and (rest L). There are two subcases: eitherN

    = 0 orN > 0:

    o Case 2.1:N = 0.

    The zeroth element ofL is simply (first L).

    o Case 2.2:N > 0.

    TheN'th member ofL 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

  • 8/2/2019 Ex LISP

    3/6

  • 8/2/2019 Ex LISP

    4/6

    (eq xy) True ifx andy evaluate to the same symbol.

    (eql xy) True ifx andy are either= oreq.

    (equal xy) True ifx andy 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 ofappend. Suppose we are given two listsL1 andL2.L1 is

    eithernil or constructed by cons.

    Case 1:L1 is nil.

    AppendingL2 toL1 simply results inL2.

    Case 2:L1 is composed of two parts: (first L1) and (rest L1). If we know the resultof appendingL2 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

  • 8/2/2019 Ex LISP

    5/6

    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, khi n=0

    Fn := F(n) := 1, khi n=1

    F(n-1) + F(n-2), 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;

  • 8/2/2019 Ex LISP

    6/6

    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?