本文介绍了在Common Lisp中进行测验?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to teach myself Spanish and Lisp. I've got several word lists likethe 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

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

The idea is to randomly include the answer with 3 randomly chosenincorrect answers. Ideally, the incorrect answers would not be toorepetitive.


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


推荐答案

使用LispWorks,并支持Unicode。

Using LispWorks, with support for 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")

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

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

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

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

计算选择-

(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

这篇关于在Common Lisp中进行测验?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:48