Common Lisp/Визначення Функцій в Ліспі/Відповіді
Common Lisp/Визначення Функцій в Ліспі:
1. a) $ (DEFUN F1 (lst)б) $ (DEFUN F2 (lst)
(CADDR (CADDDR lst))) ) (CAADR lst) )
в) $ (DEFUN FATOM (lst)г) $ (DEFUN LAST (lst)
((ATOM (CAR lst) (CAR lst)) ((CDR lst) (LAST (CDR lst)))
(FATOM (CAR lst)) ) (CAR lst) )
д) $ (DEFUN FLAST (lst)е) $ (DEFUN IN (obj lst)
((ATOM lst) lst) ((NULL lst) NIL)
((CDR lst) (FLAST (CDR lst))) ((EQL obj (CAR lst)) T)
(FLAST (CAR lst)) ) (IN obj (CDR lst)) )
2. $ (DEFUN rev (lst1 lst2)
((NULL lst1) lst2)
(PUSH (POP lst1) lst2)
(REV lst1 lst2) )
3. а) $ (DEFUN NO-DOUBLES(lst)
(NULL lst) NIL)
(CONS (CAR lst) (NO-DOUBLES (REMBER-ALL (CAR lst) lst))) )
б) $ (DEFUN UNION (lst1 lst2)
(NO-DOUBLES (APPEND lst1 lst2)) )
в) $ (DEFIN MINUS (lst1 lst2)
((NULL lst2) lst1)
(MINUS (REMBER (CAR lst2) lst1) (CDR lst2)) )
г) $ (DEFUN INTERSECT (lst1 lst2)
((NULL lst1) NIL)
((IN (CAR lst1) lst2) (CONS (CAR lst1) (INTERSECT (CDR lst1) lst2)))
(INTERSECT (CDR lst1) lst2) )
II Варіант завдань
[ред.]а) (DEFUN REVERSE_ALL (lst)
((NULL lst) NIL)
((ATOM (CAR lst)) (APPEND (REVERSE_ALL (CDR lst)) (CONS (CAR lst))))
(APPEND (REVERSE_ALL (CDR lst)) (CONS (REVERSE_ALL (CAR lst)))) )
б) (DEFUN find_neighbours (lst node)
((NULL lst) NIL)
((EQL (CAAR lst) node) (CONS (CADAR lst) (find_neighbours (CDR lst) node)))
((EQL (CADAR lst) node) (CONS (CAAR lst) (find_neighbours (CDR lst) node)))
(find_neighbours (CDR lst) node) )
в)
(DEFUN liner (lst)
((NULL lst) NIL)
((LISTP (CAR lst)) (APPEND (liner (CAR lst)) (liner (CDR lst))))
(CONS (CAR lst) (liner (CDR lst))) )
г) (DEFUN symdiff (lst1 lst2)
(UNION (minus lst1 lst2) (minus lst2 lst1)) )