# LISP homeworks

Concatenate list1 and list2.

1 2 3 4 5 6 |
(defun concat (list1 list2) (if (null list1) list2 (cons (first list1) (concat (rest list1) list2)) ) ) |

Add one element to the tail of the list.

1 2 3 |
(defun addtail (L element) (concat L (list element)) ) |

Delete list’s last element.

1 2 3 4 5 6 |
(defun deletetail (L) (if (null (rest (rest L))) (list (first L)) (cons (first L) (deletetail (rest L))) ) ) |

Delete list’s head element.

1 2 3 |
(defun deletehead (L) (rest L) ) |

L1 – L2. Return list with elements that are in L1 but not in L2.

1 2 3 4 5 6 7 8 9 |
(defun diff (L1 L2) (if (null L1) nil (if (null (member (first L1) L2)) (cons (first L1) (diff (rest L1) L2)) (diff (rest L1) L2) ) ) ) |

L1 intersect L2. Return list with elements that are in both L1 and L2.

1 2 3 4 5 6 7 8 9 |
(defun intersect (L1 L2) (if (null L1) nil (if (null (member (first L1) L2)) (intersect (rest L1) L2) (cons (first L1) (intersect (rest L1) L2)) ) ) ) |

Summation of 1 to n.

1 2 3 |
(defun sum (n) (/ (* n (+ n 1)) 2) ) |

Summation of all elements in L.

1 2 3 4 5 6 |
(defun sumlist (L) (if (null L) 0 (+ (first L) (sumlist (rest L))) ) ) |

Summation of two biggest elements in L.

1 2 3 |
(defun sum2biggest (L) (sumlist (2biggest L)) ) |

Find 2 biggest elements in L, return them in a list.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
(defun 2biggest (L) (if (= (length L) 1) L (progn (setq remain (2biggest (rest L))) (if (> (first L) (first remain)) (list (first L) (first remain)) (if (or (null (second remain)) (> (first L) (second remain))) (list (first remain) (first L)) remain ) ) ) ) ) |

(asclists ‘(73 62 32 19 22 95 98 29 31 63)) => ((73) (62) (32) (19 22 95 98) (29 31 63)).

1 2 3 4 5 6 7 8 9 10 11 12 |
(defun asclists (L) (if (= (length L) 1) (list L) (progn (setq remain (asclists (rest L))) (if (< (first L) (first (first remain))) (cons (cons (first L) (first remain)) (rest remain)) (cons (list (first L)) remain) ) ) ) ) |