emacs.d/clones/lisp/www.csci.viu.ca/~wesselsd/courses/csci330/code/lisp/intersect.cl.html
2022-10-07 19:32:11 +02:00

21 lines
670 B
HTML

#! /usr/bin/gcl -f
(defun intersect (L1 L2)
"(intersect L1 L2) returns the list of all elements appearing in both L1 and L2, removes duplicates, nil by default"
(cond
((not (listp L1)) nil)
((not (listp L2)) nil)
((null L1) nil)
((null L2) nil)
((member (car L1) (cdr L1)) (intersect (cdr L1) L2))
((member (car L1) L2) (cons (car L1) (intersect (cdr L1) L2)))
(t (intersect (cdr L1) L2))))
(format t "Enter list 1: ")
(defvar first (read))
(format t "Enter list 2: ")
(defvar second (read))
(defvar result (intersect first second))
(format t "The intersection of~% ~A and~% ~A is~% ~A~%~%" first second result)