Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lisp: CHAR is neither declared nor bound

I have decided to learn (Common) Lisp a few days ago and I realize that this is quite a newbie question and it is probably extremely trivial to someone with at least a bit of experience.

So basically what happens is that I load up Emacs + Slime (via Lisp in a Box) and write my program (included below):

(defun last-char (s) "Get last character"
       (char s (- (length s) 1)))

And then I try to compile it with C - c M - k, but then I get the following warning:

CHAR is neither declared nor bound, it will be treated as if it were declared SPECIAL.

What is the meaning of this warning? I suppose it might be something similar to forgetting #includes in C, but I can't quite figure it out. What should I do about it? Shall I just simply ignore it?

like image 668
Tamas Czinege Avatar asked Jul 27 '09 09:07

Tamas Czinege


1 Answers

The warning means that char is not being recognized as a function, as it should, for some reason (it's reporting that the symbol is unbound, it has no value).

It might have something to do with your implementation. I've run your code using C-c M-k in my SBCL + Emacs/Slime (and in Clozure) and I get the following report from the compilation in SBCL:

; in: DEFUN LAST-CHAR
;     (CHAR S (- (LENGTH S) 1))
; --> AREF 
; ==>
;   (SB-KERNEL:HAIRY-DATA-VECTOR-REF ARRAY SB-INT:INDEX)
; 
; note: unable to
;   optimize
; due to type uncertainty:
;   The first argument is a STRING, not a SIMPLE-STRING.
; 
; note: unable to
;   avoid runtime dispatch on array element type
; due to type uncertainty:
;   The first argument is a STRING, not a SIMPLE-ARRAY.

Try just typing

#'char 

on the REPL, the response should be that it is reported to be a function,

CL-USER> #'char
#<FUNCTION CHAR>

but maybe it doesn't in your implementation (I'm guessing it doesn't, given the compiler warning and the fact that the code compiles correctly in SBCL). If that's the case, then that is a point where your implementation departs from the ANSI Common Lisp specification, because that function should be there.

like image 120
Pinochle Avatar answered Oct 13 '22 23:10

Pinochle