开发者

Generating a quiz in Common Lisp?

开发者 https://www.devze.com 2022-12-20 00:07 出处:网络
I want to teach myself Spanish and Lisp.I\'ve got several word lists like the data show below.How can I generate a quiz from the data that looks like this?

I want to teach myself Spanish and Lisp. I've got several word lists like the data show below. How can I generate a quiz from the data that looks like this?

amarillo?  [ ] blue    [ ] yellow  [ ] gray   [ ] pink
azul?      [ ] red     [ ] blue    [ ] green  [ ] orange
 .
 .
 .
verde?开发者_如何学编程     [  ] purple [ ] gold   [ ] green   [ ] black

The idea is to randomly include the answer with 3 randomly chosen incorrect answers. Ideally, the incorrect answers would not be too repetitive.

amarillo|yellow
azul|blue
blanco|white
dorado|golden
gris|gray
marrón|brown
naranja|orange
negro|black
oro|gold
púrpura|purple
rojo|red
rosa|pink
verde|green


Using LispWorks, with support for Unicode.

The color translations.

(defparameter *word-data*
"amarillo|yellow
azul|blue
blanco|white
dorado|golden
gris|gray
marrón|brown
naranja|orange
negro|black
oro|gold
púrpura|purple
rojo|red
rosa|pink
verde|green")

a few top-level variables: a translation table and two lists with color names

(defvar *translation* (make-hash-table :test #'equalp))
(defvar *all-english-colors* nil)
(defvar *all-spanish-colors* nil)

Parsing the translation data and filling the table and the lists:

(defun parse-word-data (&optional (data *word-data*))
  (with-input-from-string (stream data)
    (loop for line = (read-line stream nil nil)
          while line
          do (let ((pos (position #\| line)))
               (setf (gethash (subseq line 0 pos) *translation*)
                     (subseq line (1+ pos)))
               (pushnew (subseq line 0 pos)    *all-spanish-colors*
                        :test #'equalp)
               (pushnew (subseq line (1+ pos)) *all-english-colors*
                        :test #'equalp)))))

Find the translation for a spanish word:

(defun translate-s->e (word)
  (gethash word *translation*))

Choose n random elements from a list:

(defun random-elements (list &optional (n 1))
  (let ((elements nil)
        (all-elements list))
    (loop repeat n
          for r = (random (length all-elements))
          for e = (elt all-elements r)
          do (push e elements)
          do (setf all-elements (remove e all-elements :test #'equal)))
    elements))

Choose three random english colors:

(defun random-english-colors (answer &optional (n 3))
  (random-elements (remove answer *all-english-colors* :test #'equal) n))

Compute the choices -

(defun compute-choices (answer answers &optional (n 3))
  (let ((pos (random (1+ n))))
    (append (subseq answers 0 pos)
            (list answer)
            (subseq answers pos))))

The test:

(defun test ()
  (loop for sc in *all-spanish-colors*
        for ec = (translate-s->e sc)
        do (apply #'format
                  t
                  "~%~a~1,16@T[ ] ~a~1,16@T[ ] ~a~1,16@T[ ]  ~a~1,16@T[ ] ~a"
                  sc 
                  (compute-choices ec (random-english-colors ec))))
  (terpri))

Compute the data:

(parse-word-data)

The test:

CL-USER 212 > (test)

verde           [ ] green       [ ] yellow      [ ]  orange     [ ] brown
rosa            [ ] yellow      [ ] orange      [ ]  pink       [ ] golden
rojo            [ ] gold        [ ] golden      [ ]  brown      [ ] red
púrpura         [ ] gold        [ ] red         [ ]  orange     [ ] purple
oro             [ ] orange      [ ] gold        [ ]  red        [ ] white
negro           [ ] gold        [ ] black       [ ]  golden     [ ] purple
naranja         [ ] red         [ ] gray        [ ]  orange     [ ] gold
marrón          [ ] orange      [ ] pink        [ ]  brown      [ ] red
gris            [ ] brown       [ ] green       [ ]  gray       [ ] orange
dorado          [ ] golden      [ ] pink        [ ]  blue       [ ] gray
blanco          [ ] blue        [ ] red         [ ]  white      [ ] gold
azul            [ ] brown       [ ] blue        [ ]  green      [ ] purple
amarillo        [ ] red         [ ] yellow      [ ]  brown      [ ] black
0

精彩评论

暂无评论...
验证码 换一张
取 消