Avatar of ljj038

ljj038's solution

to Sublist in the Common Lisp Track

Published at Nov 22 2019 · 0 comments
Instructions
Test suite
Solution

Given two lists determine if the first list is contained within the second list, if the second list is contained within the first list, if both lists are contained within each other or if none of these are true.

Specifically, a list A is a sublist of list B if by dropping 0 or more elements from the front of B and 0 or more elements from the back of B you get a list that's completely equal to A.

Examples:

  • A = [1, 2, 3], B = [1, 2, 3, 4, 5], A is a sublist of B
  • A = [3, 4, 5], B = [1, 2, 3, 4, 5], A is a sublist of B
  • A = [3, 4], B = [1, 2, 3, 4, 5], A is a sublist of B
  • A = [1, 2, 3], B = [1, 2, 3], A is equal to B
  • A = [1, 2, 3, 4, 5], B = [2, 3, 4], A is a superlist of B
  • A = [1, 2, 4], B = [1, 2, 3, 4, 5], A is not a superlist of, sublist of or equal to B

Setup

Check out Exercism Help for instructions to get started writing Common Lisp. That page will explain how to install and setup a Lisp implementation and how to run the tests.

Formatting

While Common Lisp doesn't care about indentation and layout of code, nor whether you use spaces or tabs, this is an important consideration for submissions to exercism.io. Excercism.io's code widget cannot handle mixing of tab and space characters well so using only spaces is recommended to make the code more readable to the human reviewers. Please review your editors settings on how to accomplish this. Below are instructions for popular editors for Common Lisp.

VIM

Use the following commands to ensure VIM uses only spaces for indentation:

:set tabstop=2
:set shiftwidth=2
:set expandtab

(or as a oneliner :set tabstop=2 shiftwidth=2 expandtab). This can be added to your ~/.vimrc file to use it all the time.

Emacs

Emacs is very well suited for editing Common Lisp and has many powerful add-on packages available. The only thing that one needs to do with a stock emacs to make it work well with exercism.io is to evaluate the following code:

(setq indent-tab-mode nil)

This can be placed in your ~/.emacs (or ~/.emacs.d/init.el) in order to have it set whenever Emacs is launched.

One suggested add-on for Emacs and Common Lisp is SLIME which offers tight integration with the REPL; making iterative coding and testing very easy.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

sublist-test.lisp

(ql:quickload "lisp-unit")
#-xlisp-test (load "sublist")

(defpackage #:sublist-test
  (:use #:common-lisp #:lisp-unit))

(in-package #:sublist-test)


;; Equal lists tests:
(define-test empty-lists
  (assert-equal :equal (sublist:sublist (list)
                                         (list))))

(define-test list-equals-itself
  (assert-equal :equal (sublist:sublist (list 1 2 3)
                                         (list 1 2 3))))


;; Unequal lists tests:
(define-test different-lists
  (assert-equal :unequal (sublist:sublist (list 1 2 3)
                                           (list 2 3 4))))

(define-test first-list-missing-element-from-second-list
  (assert-equal :unequal (sublist:sublist (list 1 3)
                                           (list 1 2 3))))

(define-test second-list-missing-element-from-first-list
  (assert-equal :unequal (sublist:sublist (list 1 2 3)
                                           (list 1 3))))

(define-test order-matters-to-a-lists
  (assert-equal :unequal (sublist:sublist (list 1 2 3)
                                           (list 3 2 1))))

(define-test same-digits-different-numbers
  (assert-equal :unequal (sublist:sublist (list 1 0 1)
                                           (list 10 1))))

;; Sublist lists tests:
(define-test empty-list-within-non-empty-list
  (assert-equal :sublist (sublist:sublist (list)
                                           (list 1 2 3))))

(define-test false-start
  (assert-equal :sublist (sublist:sublist (list 1 2 5)
                                           (list 0 1 2 3 1 2 5 6))))

(define-test consecutive
  (assert-equal :sublist (sublist:sublist (list 1 1 2)
                                           (list 0 1 1 1 2 1 2))))

(define-test sublist-at-start
  (assert-equal :sublist (sublist:sublist (list 0 1 2)
                                           (list 0 1 2 3 4 5))))

(define-test sublist-in-middle
  (assert-equal :sublist (sublist:sublist (list 2 3 4)
                                           (list 0 1 2 3 4 5))))

(define-test sublist-at-end
  (assert-equal :sublist (sublist:sublist (list 3 4 5)
                                           (list 0 1 2 3 4 5))))


;; Superlist lists tests:
(define-test not-empty-list-contains-empty-list
  (assert-equal :superlist (sublist:sublist (list 1 2 3)
                                             (list))))

(define-test at-start-of-superlist
  (assert-equal :superlist (sublist:sublist (list 0 1 2 3 4 5)
                                             (list 0 1 2))))

(define-test in-middle-of-superlist
  (assert-equal :superlist (sublist:sublist (list 0 1 2 3 4 5)
                                             (list 2 3))))

(define-test at-end-superlist
  (assert-equal :superlist (sublist:sublist (list 0 1 2 3 4 5)
                                             (list 3 4 5))))

#-xlisp-test
(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all))
(defpackage #:sublist
  (:use #:common-lisp)
  (:export #:sublist))

(in-package #:sublist)

(defun sublist (list1 list2)
  "what is list1 of list2 (sublist, superlist, equal or unequal)"
  (cond
    ((equalp list1 list2) :equal)
    ((search list1 list2) :sublist)
    ((search list2 list1) :superlist)
    (t :unequal)))

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?