如何使用"cl-position"准确地发挥作用? [英] How do I use the "cl-position" function accurately in clisp?

查看:123
本文介绍了如何使用"cl-position"准确地发挥作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在clcli中使用cl-position,但是,每当我尝试运行代码时,都会出现以下错误:

I am trying to use cl-position in clisp, however, whenever I try to run the code, I get the following error:

    Prinayas-MacBook-Pro:~ pchoubey$ clisp project1.lisp
    *** - SYSTEM::%EXPAND-FORM:
          (PRINT (CL-POSITION "bar" '("foo" "bar" "baz") :TEST 'EQUAL)) should be
          a lambda expression

为什么会发生这种情况,如何使此代码正常运行?供参考,这是我的代码:

Why is this happening and how can I get this code to run properly? For reference, here is my code:

(defun answer-ynq()
  (setq ROBOT '(IS_A_ROBOT ROBBIE))
  (loop for x in ROBOT
    do(
       (print (cl-position "bar" '("foo" "bar" "baz") :test 'equal))
    ))

  (setq KB (make-hash-table :test 'equal))

(setf (gethash '(IS_A_ROBOT ROBBIE) KB)'T)
(setf (gethash '(IS_A_PERSON BOB) KB) 'T)
(setf (gethash '(IS_CLEVER ROBBIE) KB) 'T)
(setf (gethash '(OWNS ALICE ROBBIE) KB) 'T)
)
(answer-ynq)

推荐答案

首先,我们需要使代码的格式和缩进更好

(defun answer-ynq ()
  (setq ROBOT '(IS_A_ROBOT ROBBIE))
  (loop for x in ROBOT
        do ((print (cl-position "bar" '("foo" "bar" "baz")
                                :test 'equal))))

  (setq KB (make-hash-table :test 'equal))

  (setf (gethash '(IS_A_ROBOT ROBBIE) KB) T)
  (setf (gethash '(IS_A_PERSON BOB) KB)   T)
  (setf (gethash '(IS_CLEVER ROBBIE) KB)  T)
  (setf (gethash '(OWNS ALICE ROBBIE) KB) T))

(answer-ynq)

实际错误:括号错误

((print (cl-position "bar" '("foo" "bar" "baz")
                     :test 'equal)))

这样的函数调用不能在圆括号中加上括号.通常,括号不是可选的分组字符,但是它们提供了必要的语法结构.

One can't put parentheses around a function call like that. Generally parentheses are not optional grouping characters, but they provide essential syntactic structure.

必须为:

(print (cl-position "bar" '("foo" "bar" "baz")
                    :test 'equal))

如果要在这样的loop表达式中使用多种形式,则有两个基本选项:

If one wants more than one form in such a loop expression one has two basic options:

(loop ...
      do (do-this foo bar)
         (do-that foo baz))

(loop ...
      do (progn
           (do-this foo bar)
           (do-that foo baz)))

progn特殊运算符.封闭的表格将一一评估,并返回最后的结果值.

progn is a special operator. The enclosed forms are evaluated one by one and the last result value is returned.

潜伏着更多的问题

ROBOTKB是未定义的变量.如果要引入其他局部变量,可以使用letlet*.

ROBOT and KB are undefined variables. If you want to introduce additional local variables, one can use let and let*.

cl-position是Common Lisp中的未定义函数.它实际上称为position.在Common Lisp中,标准函数永远不会带有前缀cl-.他们在Emacs Lisp中具有此前缀,Emacs Lisp缺少函数名称的名称空间,并且将该前缀用作丑陋的hack. Common Lisp具有用于函数名称的名称空间(称为 packages ),因此,像position这样的函数不需要名称前缀.对于命名空间,Common Lisp中的名称为cl:position或更长的common-lisp:position.大多数时候,人们只能写position.

cl-position is an undefined function in Common Lisp. It is actually called position. In Common Lisp the standard function never have a prefix cl-. They have this prefix in Emacs Lisp, which lacks namespaces for function names and uses the prefix as an ugly hack. Common Lisp has namespaces for function names (called packages) and thus functions like position don't need a name prefix. With the namespace, the name in Common Lisp is cl:position or longer common-lisp:position. Most of the time one can just write position.

这篇关于如何使用"cl-position"准确地发挥作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆