【问题标题】:Slime claims input error; all parentheses match史莱姆声称输入错误;所有括号匹配
【发布时间】:2013-12-30 19:14:56
【问题描述】:

我很忙:只是练习操作列表,这是我的代码:

(defun new-strand (size type)
  (if (= 0 size)
      nil
      (case type
        ((nstrand) (cons 'x (new-strand (1- size) 'nstrand)))
        ((nlist) (cons '(x) (new-strand (1- size) 'nlist))))))

(defun locate-string (string index) 
  (if (null list)
      '(Cannot locate string: invalid index)
      (if (= index 0)
          'string
          (locate-string (cdr string) (1- index)))))

(defun new-substring (string)
  (if (> (length (car string)) 1)
      (cons (new-strand (1- (length (car string))) 'nstrand) (cdr string))
      (cons '(x) (new-substring (cdr string)))))

(defun construct-string (string index)
  (append (reverse (new-substring (cdr (locate-string
                                         (reverse string)
                                         (- (length string) index)))))
          (cons (new-strand (1+ (length (car (locate-string string index))))
                            'nstrand)
                (new-substring (cdr (locate-string string index))))))

是的,我知道最后一个函数很长,但我已经手动和通过 minibuffer (check-parens) 检查了括号......它们似乎匹配。那么...有人可以告诉我为什么我会收到 SB:INPUT-ERROR-IN-COMPILE-FILE 吗?

【问题讨论】:

  • 在最后加一个括号。
  • 另外,CCL 不喜欢你的'(Cannot locate string: invalid index)。尝试将其替换为字符串“无法定位字符串:无效索引”。
  • 另外,locate-string 没有名为 list 的变量。
  • @x86ris:您可能想要安装 pareditsmartparens。一旦你使用了这样一个次要的模式,你真的必须不遗余力地得到不平衡的括号。除此之外,像 rainbow-delimiters-mode 这样的次要模式也可能会有所帮助:因为很明显,最后一个括号的颜色与 之前的括号不匹配defun ^ ^

标签: emacs input common-lisp sbcl slime


【解决方案1】:

回顾一下:

  1. 缺少尾随括号。您现在似乎已经添加了它。
  2. '(Cannot locate string: invalid index) 因冒号而窒息 :
  3. locate-string 没有变量 list。您的意思可能是 string

为什么不像这样格式化最后一个函数:

(defun construct-string (string index)
  (append 
   (reverse (new-substring (cdr (locate-string (reverse string) (- (length string) index))))) 
   (cons 
    (new-strand (1+ (length (car (locate-string string index)))) 'nstrand) 
    (new-substring (cdr (locate-string string index))))))

【讨论】:

  • 感谢您的帮助。只是想知道:是否有一个转义序列允许我在列表中使用冒号字符?编辑:nvm;就像在其他语言中一样,使用反斜杠。
  • 问题中construct-string 的格式更好,至少现在(可能会被编辑)。
  • @Svante 是的,它经过大量编辑;过去或多或少都在一行;-)
猜你喜欢
  • 2011-09-24
  • 1970-01-01
  • 2016-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多