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
a new collection containing those elements where the predicate is false.
For example, given the collection of numbers:
And the predicate:
Then your keep operation should produce:
While your discard operation should produce:
Note that the union of keep and discard is all the elements.
The functions may be called
discard, or they may need different
names in order to not clash with existing functions or concepts in your
Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.
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
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
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)) #-xlisp-test (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 (pred list &optional result) (cond ((null list) (reverse result)) ((funcall pred (first list)) (keep pred (rest list) (cons (first list) result))) (t (keep pred (rest list) result)))) (defun discard (pred list) (keep (complement pred) list))
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.