jbchichoko
jbchichoko

Reputation: 1634

Very weird error in common lisp

This is really giving me a headache

I have a following simple function written in a file.

 (defun find-num (string)
   (if (> (length string) 1)
       (parse-integer (remove (coerce (get-first-letter string)
                                      'character)
                              string))
     ;else
     1))

What it does is parse the number from string "a23" after removing the first letter. I assumed that only the first char is a letter and the rest are "numbers".

I load the file, and when I try to run the function, it give me the error, saying: A proper list must not end with "a3" ... WHAT???

But when I copy and paste the same exact code directly in the command line, the function works as it should be.

What is this ? Common lisp error ? Or is there something I am not seeing ?

 ;; Loading file C:\Users ... (hidden)
 ;; Loaded file C:\Users ... (hidden)
 T
 [2]> (find-num "a3")

 *** - ENDP: A proper list must not end with "a3"
 The following restarts are available:
 ABORT          :R1      Abort main loop
 Break 1 [3]> :a

 [4]> (defun find-num (string)
   (if (> (length string) 1)
  (coerce (get-first-letter string) 'character) string))

     ;else
     1))
 WARNING: DEFUN/DEFMACRO: redefining function FIND-NUM in top-level, was
     defined in C:\Users\.... (hidden)
 FIND-NUM
 [5]> (find-num "a3")
 3 ;
 1

Upvotes: 0

Views: 991

Answers (1)

Rainer Joswig
Rainer Joswig

Reputation: 139261

Btw.:

(parse-integer (remove (coerce (get-first-letter string)
                               'character)
                       string))

is just

(parse-integer (remove (aref string 0) string))

is better:

(parse-integer (subseq string 1))

is best

(parse-integer string :start 1)

Upvotes: 5

Related Questions