Code: Select all
(setf (fdefinition `foo) #'bar)
Code: Select all
(labels (foo (&rest args) (bar @,args)))
Is there some obscure macro that does what I'm wanting, or maybe I can use whatever such a macro would use?
Code: Select all
(setf (fdefinition `foo) #'bar)
Code: Select all
(labels (foo (&rest args) (bar @,args)))
I don't really understand what you want... if you want to establish a lexical function calling a function stored in variable, the you can just do:Jesdisciple wrote:Is there some obscure macro that does what I'm wanting, or maybe I can use whatever such a macro would use?
Code: Select all
(flet ((lexical-alias (&rest args)
(apply variable-holding-your-function args)))
...)
Code: Select all
function foo(){
function bar(){
return function (){
alert(70 * 7);
}
}
baz = bar();
}
Code: Select all
function foo(){
function bar(){
return function (){ // anonymous function
alert(70 * 7);
};
}
var baz = bar();
}
Code: Select all
function foo(){
function bar(){
return function (){
alert(70 * 7);
}
}
function baz(){
return bar();
}
}
SETF doesn't create variables, global or otherwhise. SETF sets places. Using SETF on an undefined place, in particular nonexistent variable, is undefined. Most implementations will create a special-dynamic variable, but many will signal a warning. The reason for that is that it allows catching typos in variable names. Creating global variables is done with DEFPARAMETER/DEFVAR and shouldn't really be done at runtime anyway.Jesdisciple wrote:This makes `baz' a global variable, which is what `setf' allows me to do in CL:
Javascript is essentially a mutated version of Scheme. Scheme is Lisp-1, and Common Lisp is not. You either have to assign functions to variables bound by LET, and call them with FUNCALL/APPLY, or establish a local wrapping function with FLET/LABELS.Jesdisciple wrote:JavaScript was my first language and it's the best way I know to demonstrate what I mean.
I get the difference between places, variables and functions, but I was using JS terminology at the time, where they all get muddled together. But anyway, this is where I got the idea that (setf fdefinition) makes a new function symbol:Ramarren wrote:SETF doesn't create variables, global or otherwhise. SETF sets places. Using SETF on an undefined place, in particular nonexistent variable, is undefined.
I guess I read too much into that?http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/acc_fdefinition.html wrote:An error of type undefined-function is signaled in the non-setf case if function-name is not fbound.
`bar' is a function factory which returns essentially the same function every time, but the returned function has a state in its closure which is to be modified by user code. Unless I can cache this returned function, my objective is impossible.Ramarren wrote:Javascript is essentially a mutated version of Scheme. Scheme is Lisp-1, and Common Lisp is not. You either have to assign functions to variables bound by LET, and call them with FUNCALL/APPLY, or establish a local wrapping function with FLET/LABELS.
I don't really see why the latter would be insufficient. I suppose it would be a little more verbose, but that is just a macro away. There is no performance hit from the additional function call if the wrapping function is declared INLINE.
There are no function symbols. A symbol is conceptually a structure with a number of fields. It is usually no implemented like that because for a majority of symbols some of those fields will be empty, so it would be a waste of space. But however they are implemented the specification defines five accessors, one of which is SYMBOL-FUNCTION, which holds a function. A symbol with a function in this field names the function, but there is nothing special about it. FDEFINITION is different from SYMBOL-FUNCTION in that it take a function name, which can be more than a symbol, but when given a symbol it is more or less equivalent.Jesdisciple wrote:new function symbol
Code: Select all
(defun make-accumulator ()
(let ((total 0))
(lambda (x)
(incf total x))))
(let ((variable1 (make-accumulator))
(variable2 (make-accumulator)))
(print (funcall variable1 3))
(print (funcall variable2 2))
(print (funcall variable1 4))
(print (funcall variable2 8)))
Code: Select all
(defun foo () )
(setf (fdefinition `foo) ((lambda ()
#| ... |#)))
(foo)
Well, "field" might not actually be a Common Lisp term, I just thought it would be clearer than "slot", which applies to objects anyway. I am not sure what name to whatever is that symbols contain is. They are accessible only by accessor functions, anyway.Jesdisciple wrote:I guess I was thinking of "field" rather than "symbol" then. I've seen many new terms this past week.
It might be implemented somewhat like that, but that is an implementation detail.Jesdisciple wrote:But I was actually thinking the symbol was an index in one of several hash tables
In this case I think you mean "function namespace". Scope is, more or less, the area of code where an identifier refers to a variable. You can read more about scope and extend in CLTL chapter 3.Jesdisciple wrote:(scopes) - function scope being one. Or am I misusing "scope" as well?
Not directly. You cannot actually directly bind a lambda this way, you have to create a wrapping function, possibly inline, which will go through some variable, likely established by LET. This either a concession to implementing effective compilation or because CL was designed by committee, I don't know which.Jesdisciple wrote:Would this work with LABELS/FLET as well?
D'oh, I knew that.Ramarren wrote:In this case I think you mean "function namespace". Scope is, more or less, the area of code where an identifier refers to a variable. You can read more about scope and extend in CLTL chapter 3.
Ew... But what do you mean by "a wrapping function ... [going] through some variable"?Ramarren wrote:Not directly. You cannot actually directly bind a lambda this way, you have to create a wrapping function, possibly inline, which will go through some variable, likely established by LET.
I just mean what I did in the example in the first reply of this thread. Create a local function which only calls the function stored in a variable, then you can store the lambda in the variable but call the function without FUNCALL/APPLY.Jesdisciple wrote:Ew... But what do you mean by "a wrapping function ... [going] through some variable"?