How do I use the “cl-position” function accurately in clisp?

只谈情不闲聊 提交于 2020-03-23 07:59:13

问题


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)

回答1:


first we need to make the code better formatted and indented

(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)

The actual error: wrong parentheses

((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.

It has to be:

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

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))

or

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

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

more problems are lurking

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

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.



来源:https://stackoverflow.com/questions/60767680/how-do-i-use-the-cl-position-function-accurately-in-clisp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!