Avatar of joaofnds

joaofnds's solution

to Phone Number in the Common Lisp Track

Published at Oct 22 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

Clean up user-entered phone numbers so that they can be sent SMS messages.

The North American Numbering Plan (NANP) is a telephone numbering system used by many countries in North America like the United States, Canada or Bermuda. All NANP-countries share the same international country code: 1.

NANP numbers are ten-digit numbers consisting of a three-digit Numbering Plan Area code, commonly known as area code, followed by a seven-digit local number. The first three digits of the local number represent the exchange code, followed by the unique four-digit number which is the subscriber number.

The format is usually represented as


where N is any digit from 2 through 9 and X is any digit from 0 through 9.

Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code (1) if present.

For example, the inputs

  • +1 (613)-995-0253
  • 613-995-0253
  • 1 613 995 0253
  • 613.995.0253

should all produce the output


Note: As this exercise only deals with telephone numbers used in NANP-countries, only 1 is considered a valid country code.


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.


Event Manager by JumpstartLab http://tutorials.jumpstartlab.com/projects/eventmanager.html

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 "phone")

(defpackage #:phone-number-test
  (:use #:common-lisp #:lisp-unit))

(in-package #:phone-number-test)

(define-test cleans-number
  (assert-equal "1234567890" (phone:numbers "(123) 456-7890")))

(define-test cleans-number-with-dots
  (assert-equal "1234567890" (phone:numbers "123.456.7890")))

(define-test valid-when-11-digits-and-first-is-1
  (assert-equal "1234567890" (phone:numbers "11234567890")))

(define-test invalid-when-11-digits
  (assert-equal "0000000000" (phone:numbers "21234567890")))

(define-test invalid-when-9-digits
  (assert-equal "0000000000" (phone:numbers "123456789")))

(define-test area-code
  (assert-equal "123" (phone:area-code "1234567890")))

(define-test pretty-print
  (assert-equal "(123) 456-7890" (phone:pretty-print "1234567890")))

(define-test pretty-print-with-full-us-phone-number
  (assert-equal "(123) 456-7890" (phone:pretty-print "11234567890")))

(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all :phone-number-test))
(in-package #:cl-user)
(defpackage #:phone
  (:use #:common-lisp)
  (:export #:numbers #:area-code #:pretty-print))

(in-package #:phone)

(defun numbers (number)
  (let ((clean-number (remove-if-not #'digit-char-p number)))
      ((and (= 11 (length clean-number)) (char= #\1 (char clean-number 0)))
       (subseq number 1))
      ((= 10 (length clean-number))
      (t "0000000000"))))

(defun area-code (number)
  (subseq (numbers number) 0 3))

(defun pretty-print (number)
  (let ((clean-number (numbers number)))
    (format nil "(~d) ~d-~d"
            (subseq clean-number 0 3)
            (subseq clean-number 3 6)
            (subseq clean-number 6 10))))

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?