【问题标题】:Cond inside let doesn't work properly让里面的条件不能正常工作
【发布时间】:2018-08-03 08:20:48
【问题描述】:

我遇到了一些 Common Lisp 代码的问题。我有两个类似于以下功能的功能:

(defun recursive-func (func lst num)
  (let ((test
          (some-func func
                     (first lst)
                     (first (rest lst))
                     num))
        (next
          (recursive-func func
                          (rest lst)
                          num)))

    (cond ((null (rest lst)) nil)
          ((null test) next)
          (t (cons test next)))))

(defun some-func (func a b num)
  (if (> a b)
      nil
      (funcall func a b num)))

当列表只有一个元素时,我想要recursive-func 返回nil,但它没有并调用some-func 生成评估中止,因为bnil。这是执行的痕迹:

CL-USER> (recursive-func #'(lambda(x y z) (+ x y z)) '(1 2 3 4 5) 5)
  0: (RECURSIVE-FUNC #<FUNCTION (LAMBDA (X Y Z)) {10035F6ABB}> (1 2 3 4 5) 5)
    1: (SOME-FUNC #<FUNCTION (LAMBDA (X Y Z)) {10035F6ABB}> 1 2 5)
    1: SOME-FUNC returned 8
    1: (RECURSIVE-FUNC #<FUNCTION (LAMBDA (X Y Z)) {10035F6ABB}> (2 3 4 5) 5)
      2: (SOME-FUNC #<FUNCTION (LAMBDA (X Y Z)) {10035F6ABB}> 2 3 5)
      2: SOME-FUNC returned 10
      2: (RECURSIVE-FUNC #<FUNCTION (LAMBDA (X Y Z)) {10035F6ABB}> (3 4 5) 5)
        3: (SOME-FUNC #<FUNCTION (LAMBDA (X Y Z)) {10035F6ABB}> 3 4 5)
        3: SOME-FUNC returned 12
        3: (RECURSIVE-FUNC #<FUNCTION (LAMBDA (X Y Z)) {10035F6ABB}> (4 5) 5)
          4: (SOME-FUNC #<FUNCTION (LAMBDA (X Y Z)) {10035F6ABB}> 4 5 5)
          4: SOME-FUNC returned 14
          4: (RECURSIVE-FUNC #<FUNCTION (LAMBDA (X Y Z)) {10035F6ABB}> (5) 5)
            5: (SOME-FUNC #<FUNCTION (LAMBDA (X Y Z)) {10035F6ABB}> 5 NIL 5)
; Evaluation aborted on #<TYPE-ERROR expected-type: NUMBER datum: NIL>.

希望有人能帮助我,谢谢。

【问题讨论】:

    标签: conditional common-lisp let


    【解决方案1】:

    首先评估let 中的绑定,然后才执行cond 中的测试。 你只需要改变一点:

    (defun recursive-func (func list num)
      (if (null (rest list))
          nil
          (let ((test (some-func func
                                 (first list)
                                 (first (rest list))
                                 num))
                (next (recursive-func func
                                      (rest list)
                                      num)))
            (cond ((null test) next)
                  (t (cons test next))))))
    

    注意cond也可以写成:

    (if test (cons test next) next)
    

    用你的例子:

    (recursive-func (lambda (x y z) (+ x y z))
                    '(1 2 3 4 5)
                    5)
    => (8 10 12 14)
    

    或者,您可以通过以下方式分解任务(在 REPL 中):

    > (maplist (lambda (list)
                 (list
                  (first list)
                  (second list)))
               '(1 2 3 4 5))
    => ((1 2) (2 3) (3 4) (4 5) (5 NIL))
    

    首选SECOND 而不是(first (rest ...))。 最后一个结果在 REPL 中绑定到变量*。用BUTLAST 删除最后一个(无用的)对:

    (butlast *)
    => ((1 2) (2 3) (3 4) (4 5))
    

    然后,对于这个列表中的每一对,调用你的函数——这里只是+。注意DESTRUCTURING-BIND的使用:

    (mapcar (lambda (list)
              (destructuring-bind (a b) list
                (+ a b 5)))
            *)
    => (8 10 12 14)
    

    所以,基本上,你的函数可以写成:

    (defun map-pair-funcall (function list number)
      (mapcar (lambda (pair)
                (destructuring-bind (a b) pair
                  (funcall function a b number)))
              (butlast (maplist (lambda (list)
                                  (list (first list) (second list)))
                                list))))
    

    因此:

    (map-pair-funcall #'+ '(1 2 3 4 5) 5)
    => (8 10 12 14)
    

    编辑:

    我错过了提供的函数可能返回 NIL 的情况。用(remove NIL (mapcar ...)) 包装调用mapcar 的表单以过滤掉它。


    您可以使用MAPCON 在一次迭代中执行所有这些操作。该函数迭代子列表,并连接结果列表。因此,被调用的函数应该返回列表,当您返回 NIL 时,结果会被简单地丢弃。

    让我们定义ensure-list(或使用Alexandria中的那个):

    (defun ensure-list (expr)
      (if (listp expr) expr (list expr)))
    

    该函数将返回的值包装在一个列表中,除非它已经是一个列表(特别是 NIL)。然后,函数定义如下:

    (defun map-pair-funcall (function list number)
      (mapcon (lambda (list)
                (and (second list)
                     (ensure-list (funcall function
                                           (first list)
                                           (second list)
                                           number))))
              list))
    

    你也可以LOOP:

    (defun map-pair-funcall (function list number)
      (loop 
        for (a b) on list
        for result = (and b (funcall function a b number))
        when result
          collect result))
    

    【讨论】:

    • 非常感谢。第一个解决方案对我有用,但“MAPLIST”的改进并未涵盖某些对作为参数传递的函数的调用返回“nil”的情况。它仍然在结果列表中,必须将其删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多