Recursion 在公共LISP中递归连接字符列表

Recursion 在公共LISP中递归连接字符列表,recursion,encryption,lisp,concatenation,common-lisp,Recursion,Encryption,Lisp,Concatenation,Common Lisp,因此,我尝试在LISP中递归地实现一个Caesar密码,我已经实现了基本功能。问题是它返回一个字符列表,在return语句上调用concatenate'string只返回相同的字符列表加上一个“”。我做错了什么 (defun caesar (s n) (if (null (concatenate 'list s)) '() (cons (code-char (+ n (char-code (car (concatenate 'l

因此,我尝试在LISP中递归地实现一个Caesar密码,我已经实现了基本功能。问题是它返回一个字符列表,在return语句上调用concatenate'string只返回相同的字符列表加上一个“”。我做错了什么

(defun caesar (s n) 
    (if (null (concatenate 'list s))
        '()
        (cons 
            (code-char (+ n (char-code (car (concatenate 'list s))))) 
            (caesar (coerce (cdr (concatenate 'list s)) 'string) n)
        )
    )
)

正确的方法是在某种包装器中进行字符串和列表之间的转换,然后让主函数处理列表

以下是一种使用CL的一些力量和优雅的方法:

  • 使用CLOS方法进行包装——如果是这样的话,这可能会使其不符合提交作业的资格,但这很好地证明了我认为CLOS是多么漂亮,这也是我实际编写类似内容的方式
  • 在包装器方法中使用
    强制
    ,而不是
    连接
    来更改类型,因为这就是它的用途
  • 故意不处理原始代码中有关递归和字符代码的一些其他问题
首先,这是一个使用两种方法的版本:包装器方法(为了方便起见在通用函数定义中定义),然后是执行此工作的递归方法:

(defgeneric caesar (text n)
  (:method ((text string) n)
   ;; if we're given a string just turn it into a list, then recurse
   ;; on the list & turn it back to a string (of the same type, hence
   ;; TYPE-OF).
   (coerce (caesar (coerce text 'list) n) (type-of text))))

(defmethod caesar ((text list) n)
  ;; The recursive level (note this has various issues which are in
  ;; the original code & not addressed here
  (if (null text)
      '()
    (cons (code-char (+ n (char-code (first text))))
          (caesar (rest text) n))))
其次,这里有一个稍微太聪明的方法,使用一个特殊的终止于-
null
方法。我不建议这样做,但这是CLOS可以做的事情的一个很好的演示

(defgeneric caesar (text n)
  (:method ((text string) n)
   ;; if we're given a string just turn it into a list, then recurse
   ;; on the list & turn it back to a string (of the same type, hence
   ;; TYPE-OF).
   (coerce (caesar (coerce text 'list) n) (type-of text))))

(defmethod caesar ((text null) n)
  ;; termination
  '())

(defmethod caesar ((text list) n)
  ;; The recursive level (note this has various issues which are in
  ;; the original code & not addressed here
  (cons (code-char (+ n (char-code (first text))))
        (caesar (rest text) n)))

正确的方法是在某种包装器中进行字符串和列表之间的转换,然后让主函数处理列表

以下是一种使用CL的一些力量和优雅的方法:

  • 使用CLOS方法进行包装——如果是这样的话,这可能会使其不符合提交作业的资格,但这很好地证明了我认为CLOS是多么漂亮,这也是我实际编写类似内容的方式
  • 在包装器方法中使用
    强制
    ,而不是
    连接
    来更改类型,因为这就是它的用途
  • 故意不处理原始代码中有关递归和字符代码的一些其他问题
首先,这是一个使用两种方法的版本:包装器方法(为了方便起见在通用函数定义中定义),然后是执行此工作的递归方法:

(defgeneric caesar (text n)
  (:method ((text string) n)
   ;; if we're given a string just turn it into a list, then recurse
   ;; on the list & turn it back to a string (of the same type, hence
   ;; TYPE-OF).
   (coerce (caesar (coerce text 'list) n) (type-of text))))

(defmethod caesar ((text list) n)
  ;; The recursive level (note this has various issues which are in
  ;; the original code & not addressed here
  (if (null text)
      '()
    (cons (code-char (+ n (char-code (first text))))
          (caesar (rest text) n))))
其次,这里有一个稍微太聪明的方法,使用一个特殊的终止于-
null
方法。我不建议这样做,但这是CLOS可以做的事情的一个很好的演示

(defgeneric caesar (text n)
  (:method ((text string) n)
   ;; if we're given a string just turn it into a list, then recurse
   ;; on the list & turn it back to a string (of the same type, hence
   ;; TYPE-OF).
   (coerce (caesar (coerce text 'list) n) (type-of text))))

(defmethod caesar ((text null) n)
  ;; termination
  '())

(defmethod caesar ((text list) n)
  ;; The recursive level (note this has various issues which are in
  ;; the original code & not addressed here
  (cons (code-char (+ n (char-code (first text))))
        (caesar (rest text) n)))

我很想将输出与字符串和标签结合起来(对于递归位):

(德芬·凯撒(南北方)
(输出为字符串(密码)
(标签)(牛肉)
(当s
(普林斯密码)
(其余(()))
(牛肉(胁迫(())))

警告:以上内容完全未经测试,只是简单地输入到这条消息中,因此很可能不会编译。它只是让建议更加具体。

我会尝试将输出与字符串和标签结合起来(对于递归位):

(德芬·凯撒(南北方)
(输出为字符串(密码)
(标签)(牛肉)
(当s
(普林斯密码)
(其余(()))
(牛肉(胁迫(())))

警告:以上内容完全未经测试,只是简单地输入到这条消息中,因此很可能不会编译。它只是让建议更加具体。

您的程序似乎缺少一半:specificallt
stringtolist
Oops,意外提供的旧代码,没有任何格式更新,您的代码无法读取。您可能需要格式化代码。希望您在问题上尽可能少地投入精力。您是对的,对不起,我对LISP有点陌生,应该像
(concatenate'list s)
这样做吗?以下是文档:。可能是某种强迫?但是你已经打了好几次了。我不会一直将列表转换为字符串,将字符串转换为列表。您的程序似乎缺少一半:specificallt
stringtolist
Oops,意外提供的旧代码,更新时没有任何格式,您的代码无法读取。您可能需要格式化代码。希望您在问题上尽可能少地投入精力。您是对的,对不起,我对LISP有点陌生,应该像
(concatenate'list s)
这样做吗?以下是文档:。可能是某种强迫?但是你已经打了好几次了。我不会一直把列表转换成字符串,把字符串转换成列表。