在Common Lisp中生成测验?

时间:2010-02-15 06:03:35

标签: common-lisp

我想自学西班牙语和Lisp。我有几个单词列表 数据如下所示。如何从看起来像这样的数据生成测验?

amarillo?  [ ] blue    [ ] yellow  [ ] gray   [ ] pink
azul?      [ ] red     [ ] blue    [ ] green  [ ] orange
 .
 .
 .
verde?     [  ] purple [ ] gold   [ ] green   [ ] black

这个想法是随机选择3个答案 错误答案。理想情况下,错误的答案也不会 重复的。

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

1 个答案:

答案 0 :(得分:7)

使用LispWorks,支持Unicode。

颜色翻译。

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

一些顶级变量:转换表和两个带颜色名称的列表

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

解析翻译数据并填写表格和列表:

(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)))))

找到西班牙语单词的翻译:

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

从列表中选择n个随机元素:

(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))

选择三种随机的英文颜色:

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

计算选择 -

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

测试:

(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))

计算数据:

(parse-word-data)

测试:

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