Jonathan Baxevanidis
Jonathan Baxevanidis

Reputation: 119

Error In = of (NIL 1) arguments should be of type NUMBER. in common Lisp

I have the menu which is created in the options function and the functionality of that is to have the user input a number (1 or 2 or 3) in order to solve the problem with the chosen method (DFS, BFS, BESTFS). This method is supposed to return what the user selected in this line of code (SearchProblem '(0 0 2 6 4) '(0 0 0 0 0) (Options)) in the end. The problem is that when I compile the problem it displays this error "In = of (NIL 1) arguments should be of type NUMBER.". How can I fix that?

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

Upvotes: 0

Views: 575

Answers (1)

Rainer Joswig
Rainer Joswig

Reputation: 139321

There is bunch of things you should improve:

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

Let me fix this code for you:

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

or

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

or

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

Upvotes: 6

Related Questions