Avatar of joaofnds

joaofnds's solution

to Strain in the Common Lisp Track

Published at Oct 29 2019 · 0 comments
Test suite

Implement the keep and discard operation on collections. Given a collection and a predicate on the collection's elements, keep returns a new collection containing those elements where the predicate is true, while discard returns a new collection containing those elements where the predicate is false.

For example, given the collection of numbers:

  • 1, 2, 3, 4, 5

And the predicate:

  • is the number even?

Then your keep operation should produce:

  • 2, 4

While your discard operation should produce:

  • 1, 3, 5

Note that the union of keep and discard is all the elements.

The functions may be called keep and discard, or they may need different names in order to not clash with existing functions or concepts in your language.


Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.


Check out Installing Common Lisp for instructions to get started or take a look at the guides available in the track's side bar.


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.


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 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-default indent-tabs-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.


Conversation with James Edward Gray II https://twitter.com/jeg2

Submitting Incomplete Solutions

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


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

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

(in-package #:strain-test)

(define-test empty-keep
  (assert-equal '() (strain:keep #'under-10-p '())))

(define-test keep-everything
  (assert-equal '(1 2 3) (strain:keep #'under-10-p '(1 2 3))))

(define-test keep-first-last
  (assert-equal '(1 3) (strain:keep #'oddp '(1 2 3))))

(define-test keep-nothing
  (assert-equal '() (strain:keep #'evenp '(1 3 5 7))))

(define-test keep-neither-first-nor-last
  (assert-equal '(2) (strain:keep #'evenp '(1 2 3))))

(define-test keep-strings
  (let ((strs '("apple" "zebra" "banana" "zombies" "cherimoya" "zealot")))
    (assert-equal '("zebra" "zombies" "zealot")
              (strain:keep #'starts-with-z-p strs))))

(define-test empty-discard
  (assert-equal '() (strain:discard #'under-10-p '())))

(define-test discard-everything
  (assert-equal '() (strain:discard #'under-10-p '(1 2 3))))

(define-test discard-first-and-last
  (assert-equal '(2) (strain:discard #'oddp '(1 2 3))))

(define-test discard-nothing
  (assert-equal '(1 3 5 7) (strain:discard #'evenp '(1 3 5 7))))

(define-test discard-neither-first-nor-last
  (assert-equal '(1 3) (strain:discard #'evenp '(1 2 3))))

(define-test discard-strings
  (let ((strs '("apple" "zebra" "banana" "zombies" "cherimoya" "zealot")))
    (assert-equal '("apple" "banana" "cherimoya")
              (strain:discard #'starts-with-z-p strs))))

(defun under-10-p (n) (< n 10))

(defun starts-with-z-p (s) (char= (char s 0) #\z))

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

(in-package #:strain)

(defun keep (predicate seq)
  (loop :for item :in seq
        :when (funcall predicate item)
          :collect item))

(defun discard (predicate seq)
  (keep (complement predicate) seq))

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?