如何从此文本中删除括号

时间:2015-11-27 01:09:07

标签: lisp common-lisp sbcl

所以我的导师已经给我做了一个小功能,它返回了你的星座的描述,但我对符号描述的最终输出有问题,输出仍然在括号内,我不知道我不知道怎么把它们拿出来。

(defparameter *month-signs* '((January Capricorn Aquarius 19 nil)
                   (February Aquarius Pisces 18 nil)
                    ....)))

(defparameter *sign-traits* '((Capricorn (You are a goat be happy!))
                              ....)))

(defun cookie (month-signs sign-traits)
  (format t "Please input your month of birth followed by return, and then your day of birth.
e.g january <ret> 22 <ret> Thank You. ~%")
  (let* ((month (read t));reads in month of birth
         (day (read t));reads in day of birth
         (month-assoc (assoc month month-signs)))
    (cond
      ((>=(cadddr month-assoc)day);checks if your sign is the first part of the month
       (format t "You are ~D~%" (cadr month-assoc));if it is, prints your sign and signs description
       (format t "Your sign description is: ~D~%" (cadr(assoc(cadr month-assoc) sign-traits))))
      ((< (cadddr month-assoc)22);same as above but for the other sign
       (format t "You are ~D~%" (caddr month-assoc))
       (format t "Your sign description is: ~D~%" (cadr(assoc(caddr month-assoc) sign-traits)))))))

除了这一点“(cadr(assoc(caddr month-assoc)sign-traits)”返回我想要的东西但是在括号和全部大写之外,这一切都很有用。

CL-USER> (cookie *month-signs* *sign-traits*)
Please input your month of birth followed by return, and then your day of birth.
e.g january <ret> 22 <ret> Thank You. 
january
12
You are CAPRICORN
Your sign description is: (YOU ARE A GOAT BE HAPPY)

我真的很难找到我需要摆脱最后一点(YOU ARE A GOAT BE HAPPY)所需的东西,我希望它只是打印出来“你的标志描述是:你是一只山羊,快乐。 “这可能是我错过的显而易见的东西:\ 还有一件事......只是为了你的缘故,因为变量很大并占用了大量的页面空间,我把它缩小了,因为它们都以同样的方式布局。

2 个答案:

答案 0 :(得分:4)

在列表'(January Capricorn Aquarius 19 nil)中,前三个元素是符号,由大写的REPL打印出来。符号与字符串不同。引用这样的列表时,文字被视为符号,而不是字符串。

同样,'(You are a goat be happy!)是六个符号的列表。它将作为大写符号的列表(括在括号中)打印出来。

如果用符号替换符号和列表:

(defparameter *month-signs* '((January "Capricorn" "Aquarius" 19 nil)...

(defparameter *sign-traits* '((Capricorn "You are a goat be happy!")...

你应该得到你想要的输出。

read将输入作为符号,因此您希望将关联键(1月)保留为符号。

答案 1 :(得分:4)

打印带符号的列表

Common Lisp可以通过多种方式打印符号。函数xtype: 'button', handler: function() { Ext.getCmp("mynewid").setActiveItem(1); } 为打印机提供了一个基本界面。我们不想打印字符串引号,我们想要控制单词的大写方式。

WRITE

<强> PRINC

以人类可读形式打印的标准功能是(defun write-sentence (sentence &aux ; local variable bindings (*print-escape* nil) ; no escaping (*print-readably* nil)) ; we don't print for the reader (write (first sentence) :case :capitalize) (dolist (word (rest sentence)) (write " ") (write word :case :downcase)) (write ".") (values)) ; return no values CL-USER 94 > (write-sentence '(YOU ARE A GOAT BE HAPPY)) You are a goat be happy. 。除了可选地指定输出流之外,它不提供选项。那些需要绑定在打印机控制变量中。在这里,我们需要告诉PRINC使用 case

PRINC

<强>格式

类似的功能可以使用函数(defun write-sentence (sentence) (let ((*print-case* :capitalize)) (princ (first sentence))) (dolist (word (rest sentence)) (princ " ") (let ((*print-case* :downcase)) (princ word))) (princ ".") (values)) 以更紧凑的方式编写,其中包括迭代列表的功能。这个例子留作解释的练习:

FORMAT

符号中的案例

也可以用case指定符号。它们需要在源代码中进行转义:

CL-USER 115 > (format t
                      "~{~@(~A~)~@{ ~(~A~)~}~}."
                      '(YOU ARE A GOAT BE HAPPY))
You are a goat be happy.