错误In =(NIL 1)参数应为NUMBER类型.在共同的Lisp中

Jon*_*dis 0 common-lisp

我有选项功能中创建的菜单,其功能是让用户输入一个数字(1或2或3),以解决所选方法(DFS,BFS,BESTFS)的问题.该方法应该返回用户在这行代码中选择的内容(SearchProblem'(0 0 2 6 4)'(0 0 0 0 0)(选项)).问题是,当我编译问题时,它显示此错误"In = of(NIL 1)参数应该是NUMBER类型.".我该如何解决这个问题?

;----------Otptions menu------------------------------------------------------ 
(defun Options ()
  ( print "Searching methods." )
  ( print "For DFS method press 1." )
  ( print "For BFS method press 2." )
  ( print "For BESTFS method press 3." )
  ( print "Choose searching method" )
  ( let (opt (read))
      (cond 
        ( ( = opt 1 )  'DFS )
        ( ( = opt 2 )  'BFS )
        ( ( = opt 3 )  'BESTFS ) 
      ) 
  )
)
Run Code Online (Sandbox Code Playgroud)

Rai*_*wig 5

你应该改进一些事情:

(defun Options ()                        ; Lisp does not use uppercase
  ( print "Searching methods." )         ; don't add whitespace around parentheses
                                         ; don't use print,
                                         ;   since it prints string quotes
  ( print "For DFS method press 1." )    ; users don't 'press', they 'enter'
  ( print "For BFS method press 2." )
  ( print "For BESTFS method press 3." )
  ( print "Choose searching method" )
                                         ; after print you READ
                                         ;  but you have to deliver the ouput first,
                                         ;  in case it is buffered
  ( let (opt                             ; you define two variables OPT and READ
         (read))                         ; both are set to NIL            
      (cond 
        ( ( = opt 1 )  'DFS )            ; if OPT is NIL -> ERROR. Use EQL
        ( ( = opt 2 )  'BFS )
        ( ( = opt 3 )  'BESTFS ) 
      )                                  ; no dangling parentheses in Lisp,
                                         ; this is not C
  )
)
Run Code Online (Sandbox Code Playgroud)

让我为您修复此代码:

(defun options ()
  (write-string              ; we write a multiline string
"
Searching methods.
For DFS method enter 1.
For BFS method enter 2.
For BESTFS method enter 3.

Choose searching method:
")
  (finish-output)            ; deliver all output
  (let ((opt (read)))        ; define a variable OPT
    (case opt                ; CASE uses EQL
      (1  'DFS)
      (2  'BFS)
      (3  'BESTFS))))
Run Code Online (Sandbox Code Playgroud)

要么

(defun options ()
  (write-string
"
Searching methods.
For DFS method enter 1.
For BFS method enter 2.
For BESTFS method enter 3.

Choose searching method:
")
  (finish-output)
  (case (read)
    (1  'DFS)
    (2  'BFS)
    (3  'BESTFS)))
Run Code Online (Sandbox Code Playgroud)

要么

(defparameter *option-prompt*
"
Searching methods.
For DFS method enter 1.
For BFS method enter 2.
For BESTFS method enter 3.

Choose searching method:
")

(defun options ()
  (write-string *option-prompt*)
  (finish-output)
  (case (read)
    (1  'DFS)
    (2  'BFS)
    (3  'BESTFS)))
Run Code Online (Sandbox Code Playgroud)