【问题标题】:Displaying a List of Strings Instead of a Displaying One String显示字符串列表而不是显示一个字符串
【发布时间】:2016-11-30 05:42:33
【问题描述】:

这只是 Common Lisp 中的凯撒密码,旋转密钥设置为 5。

我有一些限制
必须以列表的形式递归地读取和处理输入。
不允许使用变量、数组、循环、progn。
输出必须是字符串而不是列表。
程序必须只使用递归。

(defun encode (expr)   ; define function funcName (argument)
  ; Out case when the list is empty
  (cond ((null expr) nil)     ; conditional (test1 action1)
  ; Checking if the expression is an atom only then to go encryption
  ((atom expr)  (encrot expr))  ; test2 see if one or less atom
  ; Adding the result of encrot to the list and
  (t(cons (encode(car expr)) (encode(cdr expr)))))) ; will run if all others fail

(defun encrot (expr)
  ; casts the object and then shifts the char by 5
  (string (int-char(encrot2 (+ 5 (char-int(char (string expr) 0)))))))

(defun encrot2 (x)
  ; Checking to see if the atom is a letter
  (cond (( > x 90) (+ 64 ( mod x 90 )))
  (( < x 91) x)))

我的理解是函数 cons 将列表的元素显示为字符串。例如(“A”“B”“C”)

作为一个字符串,理论上它应该像这样"A B C"

我使用 GNU CLISP 2.49 (2010-07-07) http://clisp.cons.org/ 编译。

【问题讨论】:

  • 你能举个例子吗(输入+输出)?
  • cons 没有显示任何内容。它没有做任何输出。 cons 只是在构造一个 cons 单元格。

标签: string common-lisp clisp


【解决方案1】:

将输出列表转换为字符串的最简单方法是使用格式:

(format t "~{~a ~}" '(a b c)) => A B C

【讨论】:

  • 除非你真的想要打印副作用,否则最好使用(format nil ...)
  • @Vatine 更好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
  • 2015-08-28
相关资源
最近更新 更多