简体   繁体   中英

Lisp strange segmentation fault probably eval error

I can't understand why this code that defun a function name cause a segmenation fault. (fdefinition 'realname) --> Output of my function with no error

(eval (setf (fdefinition name) `(lambda (this ,@args) ,@body)))

but if I do (realname param) I get this error Error: Segmentation violation(11) [code 0] at 8B238080

where is my error? Thanks

The error in your code is that you're assigning a list as a fdefinition instead of a function. I would consider it a bug in your implementation though that this is causing a segfault. (I'm not sure if it is actually a bug in the sense that it's violating the standard, but it would definitely be much nicer if the implementation would catch and report this error itself.)

To turn the list starting with lambda into a function, you need to evaluate it. On the other hand, the eval around the setf doesn't seem to be necessary at all. So a possible solution is to switch the positions of the eval and setf :

(setf (fdefinition name) (eval `(lambda (this ,@args) ,@body)))

Try

(eval `(setf (fdefinition ',name) #'(lambda (this ,@args) ,@body)))

or better still use a macro instead of a function and avoid the EVAL :

(defmacro mymacro (name args &body body)
  `(setf (fdefinition ',name) #'(lambda (this ,@args) ,@body)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM