http://art2.ph-freiburg.de/art/pp 在线练习lisp

DataTypes
    Atoms
        
BREAD  jenny  12  a  B  5  0.3  x?  !y
        
(a b c) is a list
        (ab is an unclosed list
        ab c de Here are several atoms
        5,6 The comma is not accepted 
        
16)( The parentheses are non-matching. 
    S-Atoms(Symbolic Atoms)
  s-atoms represent may begin with characters or digits(but if it is a digit then the atom is a number) but may not contain a colon <<:>> or semi-colon <<;>> nor parentheses or spaces.
    Numbers
    Lists
        
With LISP the ordering of elements in a list does matter.This distinguishes lists from sets whose ordering of elements makes no difference. Another difference from sets is that elements in a list can occur several times, while in a set every element occurs only once.So then,a list consists of a pair of parentheses with the elements contained inside.
 
Nested Lists
    Empty List,NIL,and T 
          
()is the symbol NIL and the empty list
   
(()) is a list containing the empty list
   
NIL is a symbol (and therefore an atom) and the empty list ()
   
T is a symbol (and therefore an atom) 
   
7+4 is a symbol Atom
  
(7+4) is a list with one element
Functions
 Arithmetic Functions
  
 
List-access Functions
   FIRST  supplies the first element of the list.
   (FIRST (BREAD COFFEE MILK SUGAR))   ;Error A function BREAD is not defined 
   (FIRST (QUOTE (BREAD COFFEE MILK SUGAR))) <---> (FIRST '(BREAD COFFEE MILK SUGAR))     BREAD 
        (REST (BREAD COFFEE MILK SUGAR))   ;Error A function BREAD is not defined 
   (REST '(BREAD COFFEE MILK SUGAR))   (COFFEE MILK SUGAR)  
  REST  expects an argument of the type list and returne the list without the first element.
   (REST '(() ())) --->(NIL)
   (REST '((1 2 3) (4 5 6))) ---> ((4 5 6))
   (FIRST '(REST (BREAD COFFEE MILK SUGAR))) ---> REST
   (FIRST (REST '(BREAD COFFEE MILK SUGAR))) ---> COFFEE
self-defined Functions
 Defining Functions 
  (DEFUN <NAME> (<PARAMS...>) <FUNCTION BODY...>) 
  (DEFUN MY-THIRD (LIST) (FIRST(REST(REST LIST)))) 
List Constructing Functions
 CONS   (CONS <EXPRESSION> <LIST>)
  (CONS 'ALL '(MY MONEY)) ---> (ALL MY MONEY)
  (CONS '(ALL) '(MY MONEY)) ---> ((ALL) MY MONEY)
  (CONS 'HELLO 
NIL) --->(HELLO)
  (CONS (FIRST L2) (REST L1))
 LIST   (LIST <EXPRESSION> <EXPRRESSIONS...>)
  (LIST 1 2 3 '(ROSS)) ---> (1 2 3 (ROSS))
  (LIST 
NIL '(A)) ---> (NIL (A))
  (LIST 'BEACH) ---> (BEACH)
 
APPEND  (APPEND <LIST> <LISTS...>)
  (APPEND '(COME (FROM)) '(ZOG)) ---> (COME (FROM) ZOG)
  (APPEND '(I) NIL '(DO NOT BELEIVE IT)) ---> (I DO NOT BELEIVE IT)
Further List Functions
  (DEFUN LAST-ELEMENT (LIST) (FIRST (REVERSE LIST)))
  (DEFUN REPLACE-THIRD (ITEM LIST) 
       (CONS (FIRST LIST) (CONS (FIRST (REST LIST)) CONS ITEM (REST (REST (REST LIST))))))
Predicates 
 General Predicates
  ATOM   (ATOM <Expression>) 
  CONSP   (CONSP <Expression>)  
tests if an argument is a non-empty list
  
(CONSP NIL)--->NIL (CONSP '(NIL))--->T  (CONS NIL NIL)--->(NIL)
  LISTP   (LISTP <Expression>)   
tests whether a LISP expression is a list
   (LISTP NIL)--->T  (LISTP 12)--->NIL  (LISTP T)--->NIL
  NUMBERP and SYMBOLP 
  
EQUAL (EQUAL <Expression1> <Expression2>)
  (DEFUN COLD-P (ARG) (EQUAL ARG 'COLD))
  
ENDP (ENDP <LIST>)  
tests whether its argument, which has to be a list, is an empty list, or NIL
  ZEROP (ZEROP <Number>) 
tests whether the argument, which has to be a number, is the number Zero
  MEMBER (MEMBER <EXPRESSION> <LIST>) 
checks whether an atomical LISP expression is contained in a list
   (MEMBER 'I '(A I O U))--->(I O U)  (MEMBER 'B '(A I O U))--->NIL  (MEMBER 'X '(A (X) C))--->NIL 
 Arithmetic Predicates
  (>/</= <NUMBER1> <NUMBER2>)
  (DEFUN LARGER-15-P (NUMBER) (> NUMBER 15))
Logical Functions
 NOT   
all LISP expressions except NIL evaluate to NILNIL, however evaluates to T.anything in LISP that is not explicitly NIL or the empty list or if the evaluation does not evaluate to NIL, e.g. (EQUAL T NIL)) has the truth value T
 OR    
tests the truth value of its arguments from the left to the right (from argument1 to argumentN) and returns the value of the first argument that does not evaluate to NIL.LISP terminates the evaluation of OR immediately after the first "true" argument. Nothing after that is evaluated.
  (OR 'A 'B)--->A 
 AND   tests the truth value of its arguments in turn and returns NIL if an argument has the value NIL.
  (AND (FIRST '(A B C)) (REST '(E F G)))--->(F G)
  (AND (NUMBERP X) (NUMBERP Y) (> X Y))
Control Structures 

 (IF <TEST>
   <THEN-CLAUSE>
   <ELSE-CLAUSE>)
    (DEFUN WEATHER-TEST (WEATHER) (IF (EQUAL WEATHER 'GOOD) 'GO-SWIMMING 'STUDY-AT-HOME))
 (COND
  (<TEST1>...<CONSEQUENCE1>)
  (<TEST2>...<CONSEQUENCE2>).....)
    (COND ((CONSP 2) 'LISTE) ((SYMBOLP 2) 'ATOM) (T 'ZAHL)) 最后的判别式总为T
RECURSION
 (DEFUN MY-MEMBER(EXPRESSION LI)
   (COND ((ENDP LI) NIL) ((EQUAL EXPRESSION (FIRST LI)) LIST) (T (MY-MEMBER EXPRESSION (REST LI)))))

 (TRACE MY-MEMBER) (UNTRACE MY-MEMBER) 
 (DEFUN <RECURSIVE-FUNCTION> (<PARAM1>...<PARAMN>)
  (COND (<TERMINATING-CONDITION1> <RESULT1>) ...(<TERMINATING-CONDITIONN> <RESULTN>)...
      (<RECURSIVE-CONDITION1> <RECUSIVE-CALL1>) ...(<RECURSIVE-CONDITIONM> <RECURSIVE-CALLM>)))
 (DEFUN TOP-LIST (LI) (COND ((ENDP LI) T) ((CONSP (FIRST LI)) NIL) (T (TOP-LIST (REST LI))))) 

 (DEFUN CONTAINS (LI) (COND ((ENDP LI) NIL) ((NUMBERP (FIRST LI)) T) (T (CONTAINS (REST LI)))))
 (DEFUN MONOTONIC-DECRESING-P (NUMBERL)
   (COND ((ENDP NUMBERL) T) ((ENDP (REST NUMBERL)) T) ((< (FIRST NUMBERL) (SECOND NUMBERL)) NIL)
   (T (MONOTONIC-DECRESING-P (REST NUMBERL)))))
 (DEFUN X-IN-LIST-P (LI)
   (COND ((ENDP LI) NIL) ((EQUAL 'X (FIRST LI)) T) (T (X-IN-LIST-P (REST LI))))) 
 
(DEFUN ALL-SAME-AS-ELEMENT (L1 L2)
   (COND ((ENDP L2) T) ((NOT (EQUAL L1 (FIRST L2))) NIL) (T (ALL-SAME-AS-ELEMENT L1 (REST L2)))))
Top-Level-Recursion
 List Reconstruction
  (DEFUN REPLACE (EXPR1 EXPR2 LIST) 
   
(COND ((ENDP LIST) NIL)
 ((EQUAL (FIRST LIST) EXPR1) (CONS EXPR2 (REPLACE EXPR1 EXPR2 (REST LIST)))) 
     (T 
(CONS (FIRST LIST) (REPLACE EXPR1 EXPR2 (REST LIST)))))) 
  (DEFUN REMOVE-FIRST (EXPR LIST)
   (COND ((ENDP LIST) NIL) ((EQUAL (FIRST LIST) EXPR) (REST LIST)) 
     (T 
(CONS (FIRST LIST) (REMOVE-FIRST EXPR (REST LIST))))))
  Counting Functions
    (DEFUN COUNT-SUBLISTS (LIST) (COND ((ENDP LIST) 0) ((CONSP (FIRST LIST)) (+ 1 (COUNT-SUBLISTS (REST LIST)))) 
       (T (COUNT-SUBLISTS (REST LIST)))))
    (DEFUN MY-LENGTH (L1)
       (COND ((ENDP L1) 0) ((CONSP (FIRST L1) (+ 1 (MY-LENGTH (REST L1)))) (T (+ 1 MY-LENGTH (REST L1))))))
    (DEFUN REMOVE (L1 L2)
       (COND  ((ENDP L2) NIL) ((NOT (EQUAL L1 (FIRST L2))) (CONS (FIRST L2) (REMOVE L1 (REST L2)))) (T (REMOVE L1 (REST L2)))))
    
(DEFUN REPLACE-FIRST (L1 L2 L3)
       (COND((ENDP L3) NIL)((EQUAL L1 (FIRST L3))(CONS L2 (REST L3)))(T (CONS (FIRST L3)(REPLACE-FIRST L1 L2 (REST L3)))))) 
    (DEFUN test-list (L1 L2)
       (COND ((ENDP L2) NIL) ((EQUAL L1 (FIRST L2)) (CONS T (test-list L1 (REST L2)))) (T (CONS NIL (test-list L1 (REST L2))))))
  Tree-Recursion
    (defun deep (ex lst)
       (cond ((endp lst) nil) ((equal (first lst) ex) t) ((atom (first lst)) (deep ex (rest lst))) (t (or (deep ex (first lst)) (deep ex (rest lst))))))
    (defun sub (e1 e2 ls)
       (cond ((endp ls) nil)    ((equal (first ls) e1) (cons e2 (sub e1 e2 (rest ls))))   ((atom (first ls)) (cons (first list) (sub e1 e2 (rest ls))))
              (t (cons (sub e1 e2 (first ls)) (sub e1 e2 (rest ls))))))
    (defun count (lst)
       (cond ((endp lst) 0) ((atom (first lst)) (+ 1 (count (rest lst)))) (t (+ (count (first lst)) (count (rest lst))))))
    (defun anywhere-x-p (lst)
       (cond ((endp lst) nil) ((equal 'x (first lst)) t) ((not(atom (first lst))) (or (anywhere-x-p (first lst)) (anywhere-x-p (rest lst))))
          (t (anywhere-x-p (rest lst))))) 
    
(defun deep-remove (ls1 ls2)
       (cond  ((endp ls2) nil)  ((equal ls1 (first ls2)) (deep-remove ls1 (rest ls2)))  ((atom (first ls2)) (cons (first ls2) 
          (deep-remove ls1 (rest ls2))))
  (t (cons (deep-remove ls1 (first ls2)) (deep-remove ls1 (rest ls2))))))
    (defun sum-all (ls)
       (cond  ((endp ls) 0) ((atom (first ls)) (+ (first ls) (sum-all(rest ls))))  (t (+ (sum-all (first ls)) (sum-all (rest ls))))))
    (defun count-numbers (ls)
       (cond ((endp ls) 0) ((not (atom (first ls))) (+ (count-numbers(first ls)) (count-numbers(rest ls))))
           ((numberp (first ls)) (+ 1 (count-numbers (rest ls)))) (t (count-numbers (rest ls)))))
    (defun sum-numbers (ls)
       (cond ((endp ls) 0) ((not (atom (first ls))) (+ (sum-numbers (first ls)) (sum-numbers (rest ls))))
           ((numberp (first ls)) (+ (first ls) (sum-numbers (rest ls)))) (t (sum-numbers (rest ls)))))
    (DEFUN COUNT-ITEM (L1 L2)
       (COND ((ENDP L2) 0) ((EQUAL L1 (FIRST L2)) (+ 1 (COUNT-ITEM L1 (REST L2)))) ((ATOM (FIRST L2)) (COUNT-ITEM L1 (REST L2)))
          (T (+ (COUNT-ITEM L1 (FIRST L2)) (COUNT-ITEM L1 (REST L2))))))
    (DEFUN LIST-UP-TO-ATOM (LS)
       (COND  ((ENDP LS) NIL)  ((ATOM (FIRST LS)) (LIST (FIRST LS)))  (T (CONS (FIRST LS) (LIST-UP-TO-ATOM (REST LS))))))
posted @ 2016-11-10 11:36  fyk1Ex  阅读(125)  评论(0)    收藏  举报