【问题标题】:Stack overflow with assoc and a function test带有 assoc 和函数测试的堆栈溢出
【发布时间】:2014-05-29 02:10:46
【问题描述】:

我写了这个函数,如果给定的参数匹配,则返回 t,否则返回 nil。

(defun match (input-form pattern)
  (cond
    ((and (not input-form) (not pattern)) t)
    ((not pattern) nil)
    ((eq (car pattern) '*) (or (match input-form (cdr pattern)) (match (cdr input-form) pattern)))
    ((not input-form) nil)
    ((and (> (length (string (car pattern))) 1) (eq (char (string (car pattern)) 0) '#\?))
            (match (cdr input-form) (cdr pattern)) )
    ((eql (car input-form) (car pattern)) (match (cdr input-form) (cdr pattern))) ) )

当我这样做时:

(setq patterns 
    '(((bonjour *) bonjour) ((salut *) salut)) )

(assoc '(bonjour Eliza) patterns :test #'match)

它运行良好并返回:((bonjour *) bonjour)

和(assoc '(hello Eliza) patterns :test #'match) 效果也很好,返回 nil。

但是当我像这样向变量模式添加模式时:

(setq patterns 
'(((bonjour *) bonjour) ((salut) salut) ((* mere * pere *) parlez-moi de vos parents) ((mere *) la mere de qui) ((* mere) parlez-moi de votre mere)) )

当我请求匹配的东西时,它可以工作,但是当我请求不匹配的东西时,我收到堆栈溢出错误消息。

我做错了什么?

【问题讨论】:

    标签: lisp stack-overflow


    【解决方案1】:

    如果你(trace match),你会马上得到答案(只要记住足够快地按Ctrl-C :-):

      0: (MATCH (BONJOUR2 ELIZA) (BONJOUR *))
      0: MATCH returned NIL
      0: (MATCH (BONJOUR2 ELIZA) (SALUT))
      0: MATCH returned NIL
      0: (MATCH (BONJOUR2 ELIZA) (* MERE * PERE *))
        1: (MATCH (BONJOUR2 ELIZA) (MERE * PERE *))
        1: MATCH returned NIL
        1: (MATCH (ELIZA) (* MERE * PERE *))
          2: (MATCH (ELIZA) (MERE * PERE *))
          2: MATCH returned NIL
          2: (MATCH NIL (* MERE * PERE *))
            3: (MATCH NIL (MERE * PERE *))
            3: MATCH returned NIL
            3: (MATCH NIL (* MERE * PERE *))
              4: (MATCH NIL (MERE * PERE *))
              4: MATCH returned NIL
              4: (MATCH NIL (* MERE * PERE *))
                5: (MATCH NIL (MERE * PERE *))
                5: MATCH returned NIL
                5: (MATCH NIL (* MERE * PERE *))
    

    即,您需要测试(null input-form)在单步执行该模式:

    (defun match (input-form pattern)
      (cond
        ((and (null input-form) (null pattern)) t)
        ((or (null pattern) (null input-form)) nil)
        ((eq (car pattern) '*) 
         (or (match input-form (cdr pattern)) 
             (match (cdr input-form) pattern)))
        ((and (> (length (string (car pattern))) 1) 
                 (char= (char (string (car pattern)) 0) #\?))
         (match (cdr input-form) (cdr pattern)))
        ((eql (car input-form) (car pattern))
         (match (cdr input-form) (cdr pattern)))))
    

    请注意我对您的代码所做的缩进和空格更改,以及在检查空列表时使用null 而不是not(也可以考虑使用endp)。

    【讨论】:

    • 感谢您的回复;) 首先感谢您对缩进和使用 null 的建议。我也尝试过跟踪匹配,但不考虑使用 assoc 进行跟踪和测试。不幸的是,它仍然不起作用,因为 (Hello Eliza) 与 (Hello *) 不匹配,但我正在尝试修复它。
    • 好的,我添加了这个测试: ((and (eq (car pattern) '*) (eq (cdr pattern) nil)) t) 以避免当模式以 * 结尾并且它有效时出现问题,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    • 2011-12-11
    • 2011-10-26
    • 2017-10-20
    • 2019-05-18
    • 2012-07-08
    相关资源
    最近更新 更多