; your code goes here
(in-package #:cl-user)
(defpackage #:phrase
  (:use #:cl)
  (:export #:word-count))
(in-package #:phrase)

(defun word-count (phrase)
  (let (words word-start word-end)
    (labels ((update-word ()
               (let* ((word (string-downcase (subseq phrase word-start word-end)))
                      (count (assoc word words :test #'string-equal)))
                 (if (null count)
                     (push (cons word 1) words)
                     (incf (cdr count))))
               (reset-word))
             (reset-word ()
               (setf word-start nil
                     word-end nil))
             (has-word ()
               (not (equal word-end word-start))))
      (loop for c across phrase
         for i = 0 then (1+ i)
         if (alphanumericp c) do
           (if (null word-start)
               (setf word-start i
                     word-end (1+ i))
               (incf word-end))
         else if (has-word) do
           (update-word)
         finally
           (when (has-word)
             (update-word))
           (return words)))))
           
(print (word-count "Fuck this stupid shit. Fuck!"))