Lisp: CHAR is neither declared nor bound

有些话、适合烂在心里 提交于 2019-12-12 11:24:13

问题


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?


回答1:


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.




回答2:


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

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


来源:https://stackoverflow.com/questions/1187262/lisp-char-is-neither-declared-nor-bound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!