(NIL 1)个参数的In =的错误应为NUMBER类型。常见的Lisp

时间:2018-12-08 11:47:42

标签: 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 ) 
      ) 
  )
)

1 个答案:

答案 0 :(得分:5)

有很多事情需要改进:

int.Parse()

让我为您修复此代码:

str

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

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