Avatar of gokmen

gokmen's solution

to Scrabble Score in the Common Lisp Track

Published at Mar 31 2020 · 0 comments
Test suite

Given a word, compute the scrabble score for that word.

Letter Values

You'll need these:

Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10


"cabbage" should be scored as worth 14 points:

  • 3 points for C
  • 1 point for A, twice
  • 3 points for B, twice
  • 2 points for G
  • 1 point for E

And to total:

  • 3 + 2*1 + 2*3 + 2 + 1
  • = 3 + 2 + 6 + 3
  • = 5 + 9
  • = 14


  • You can play a double or a triple letter.
  • You can play a double or a triple word.


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.


Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

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 "scrabble-score")

(defpackage #:scrabble-score-test
  (:use #:cl #:lisp-unit))

(in-package #:scrabble-score-test)

(define-test no-word-has-zero-score
  (assert-equal 0 (scrabble-score:score-word "")))

(define-test whitespace-only-also-has-zero-score
  (assert-equal 0 (scrabble-score:score-word
                   (concatenate 'string '(#\Space #\Newline)))))

(define-test scores-small-word
  (assert-equal 1 (scrabble-score:score-word "a")))

(define-test is-case-insensitive
  (assert-equal 1 (scrabble-score:score-word "A")))

(define-test scores-a-slightly-bigger-word
  (assert-equal 2 (scrabble-score:score-word "at")))

(define-test scores-a-middle-of-the-road-word
  (assert-equal 6 (scrabble-score:score-word "street")))

(define-test scores-a-peculiar-word
  (assert-equal 22 (scrabble-score:score-word "quirky")))

(define-test scores-a-very-long-word
  (assert-equal 27 (scrabble-score:score-word "UNEXCLUSIVENESS")))

(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all :scrabble-score-test))
(defpackage #:scrabble-score
  (:use #:cl)
  (:export #:score-word))

(in-package #:scrabble-score)

(ql:quickload "str")

(defun score-word (word)
  (reduce (lambda (score letter)
            (+ score (cond ((not (str:letters? letter)) 0)
                           ((str:contains? letter "QZ") 10)
                           ((str:contains? letter "JX") 8)
                           ((str:contains? letter "K") 5)
                           ((str:contains? letter "FHVWY") 4)
                           ((str:contains? letter "BCMP") 3)
                           ((str:contains? letter "DG") 2)
                           (t 1))))
          (str:split "" (str:upcase word))
          :initial-value 0))

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?