Avatar of rabuf

rabuf's solution

to Scrabble Score in the Common Lisp Track

Published at Jan 20 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)


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


(defconstant +tile-values+
  (let ((h (make-hash-table :test 'equalp)))
       for (letters value) in '(("AEIOULNRST" 1)
                                ("DG" 2)
                                ("BCMP" 3)
                                ("FHVWY" 4)
                                ("K" 5)
                                ("JX" 8)
                                ("QZ" 10))
       do (loop
            for l across letters
             do (setf (gethash l h) value)))

(defun score-word (string)
     for l across string
     sum (gethash l +tile-values+ 0)))

Community comments

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

rabuf's Reflection

Looking at other solutions, I see that my hash table approach is not common. Most used `case` or an association list or similar techniques. I'll leave this as it is, though.