views:

265

answers:

2

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?

+4  A: 

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.

Pinochle
Thanks a lot! Turned out that the function was there, I just overlooked a typing mistake in an other function and that's what caused the problems. The reason your answer was very helpful is because it reassured me that the problem was indeed in my code and not somewhere else.
DrJokepu
That's why you always copy/paste code in questions!
Nowhere man
+1  A: 

Just a note regarding your indentation. Here's a more conventional indentation:

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