Are there any ways to jump to functions defined in top level let form by slime-edit-definition?
For example below, I want to jump to the head of (defin foo ...) form, not that of top level (let ...) form:
(let (var)
(defun foo ()
var)))
Related
I want to create an interpreted function definition, not a compiled one.
SBCL manual says :
Variable: *evaluator-mode* [sb-ext] : Toggle between different evaluator
implementations. If set to :compile, an implementation of eval that
calls the compiler will be used. If set to :interpret, an interpreter
will be used.
So, I try to create a BAR function (which does not exist) :
(let ((sb-ext::*evaluator-mode* :interpret))
(defun bar (x) (+ x 1)))
But then, I check, and BAR is already compiled :
CL-USER> (compiled-function-p #'bar)
T
So, how do you create an interpreted version of BAR ?
The let form in your question only sets the evaluator mode at runtime. By then, the function has already been compiled.
You need to set it at load time and also make sure to load the file instead of compiling then loading it.
Try this:
In your-file.lisp:
;; at load time, set evaluator mode to interpret (before bar definition is met)
(eval-when (:load-toplevel :execute)
(setf sb-ext::*evaluator-mode* :interpret))
;;define your interpreted function
(defun bar (x)
(+ x 1))
;; set evaluator back to compile mode (optional)
(eval-when (:load-toplevel :execute)
(setf sb-ext::*evaluator-mode* :compile))
;;check if bar is a compiled function
(print (compiled-function-p #'bar)) ;;prints NIL
Then load it with (load "your-file.lisp") (this doesn't compile the file first).
I think that *evaluator-mode* is pretty inherently a global variable. For instance, if you do this:
> (setf sb-ext:*evaluator-mode* ':interpret)
:interpret
> (setf (symbol-function 'bar)
(lambda (x) x))
#<interpreted-function nil {10026E7E2B}>
> (compiled-function-p #'bar)
nil
you get an interpreted function. But if you do this:
> (setf sb-ext:*evaluator-mode* ':compile)
:compile
> (setf (symbol-function 'bar)
(let ((sb-ext:*evaluator-mode* ':interpret))
(lambda (x) x)))
#<function (lambda (x)) {52C3687B}>
> (compiled-function-p #'bar)
t
You don't. My take on this, which may be wrong, is that the value which is in effect at the start of each top-level form is what counts: once the system has decided that it's going to use the compiling-evaluator for a form then it can't change its mind.
And note that there is a complicated definition of 'top-level form', and in particular that when processing a file then in a form like
(let (...)
(x ...))
then (x ...) is not a top-level form.
Test #1
I have a globally declared variable f, and a function with argument named f:
(defvar f 1)
(defun test (f)
f)
I call the function and it returns the value of the argument:
(test 2)
=> 2
Test #2
I again have a globally declared variable f, and a function with argument named f. This time, however, the function returns a lambda, which returns f:
(defvar f 1)
(defun test (f)
#'(lambda ()
f))
I call the function and it returns the lambda function. Then I call the lambda function and it returns the value of the global f:
(funcall (test 2))
=> 1
I am surprised. I thought the lambda function is a closure and would return the local f, not the global f. How do I modify the test function and/or the lambda function so that the lambda function returns the local f, not the global f?
A pointer to an online resource that discusses this particular scoping issue would be appreciated.
By using defvar you are declaring f a special (aka dynamically bound) variable. f in your code from there on are no longer lexically closed but in fact the same as the global variable momentarily changed to 2.
Because of this feature lispers are not happy about global variables without their *earmuffs*. Once they have *earmuffs* it's much easier to see it:
(defvar *f* 1) ; special variable *f*
(defun test (*f*) ; momentarily rebind *f*
(format nil "*f* is ~a~%" *f*) ; use new value
#'(lambda () ; return lambda using *f*
*f*)) ; *f* goes back to being 1
(funcall (test 2)) ; ==> 1 (prints "*f* is 2\n")
So the lesson is: Never make global variables without *earmuffs* since you will get crazy runtime errors which are almost impossible to detect. This naming convention isn't just for fashion!
As for documentation the hyperspec actually shows how dynamic variables work in the examples for defparameter and defvar. See that they call (foo) => (P V) and that foo re-binds *p* and *v* during its call to bar and, since they are dynamically bound, bar uses the altered values.
I am trying to implement a Binary Search Tree in SML. I have an insert function and I am trying to implement another function that takes a list and calls the insert function on each element in the list. This is what I have so far,
fun insertB (l) = insert (hd(l), Node(insertB(tl (l)), Nil, Nil))
but i don't have a base case, so thats one problem. My input function takes an int and a Node as parameters. The error I am currently getting is error right-hand-side of clause doesn't agree with function result type [tycon mismatch]
The base case for your insertB function is the empty list []. What binary search tree would correspond to that? An empty one, of course, which in your case seems to be named Nil:
fun insertB [] = Nil
That was the base case of the recursion. You now need the recursive case, which is pretty similar to what you did, except you don't try to build the BST, but rather have the recursive call build it:
| insertB (head :: tail) = insert (head, insertB tail)
The whole function is thus:
fun insertB [] = Nil
| insertB (head :: tail) = insert (head, insertB tail)
Alternatively, you can use foldl:
fun insertB xs = foldl insert Nil xs
Suppose I have to define a function called foo. Suppose that, in order to define it, I use some auxiliary functions foo1, foo2, foo3, ...
When I load the file containing those functions, from the top-level I can use all of them. Instead I want to "see" from the top-level only the function foo and "hide" the others. How can I achieve this result?
You can use flet.
E.g.,
(flet ((foo1 (...) ...)
(foo2 (...) ...)
(foo3 (...) ...))
(defun foo (...)
(foo1 (foo2 (foo3 ...))))
(defun goo (...)
(foo2 (foo1 (foo3 ...)))))
In chapter 3 of Practical Common Lisp book there's an example of a SQL-like select and where functions. Here's a simplified version of it:
(defun where (x)
#'(lambda (item)
(> item x)))
and it is used like this:
(remove-if-not (where 2) (list 1 2 3 4))
Earlier in the book it is explained that the #' sequence is used to state that it is followed by a function name, rather than a variable that requires evaluation. I don't understand why it's needed here. I tried implementing the where function without it and it worked as well:
(defun where (x)
(lambda (item)
(> item x)))
I tried googling for it, and, as you can imagine, with such a sequence of characters it wasn't a very fruitful search. And I don't know the name of this thing.
Is there any particular reason why it's needed in the above code?
This is the precise page in Hyperspec which deals with the standard macro character "sharp" followed by "single quote".
To make it simple, this reader macro expands to enclose the following form into (function <form>) s-expression. This effectively tells the parser that the form is callable.
lambda is a macro, which generates the code, which already contains the (function <form>), but historically and for consistency, the alternative form, which is obtained from the reader macro with sharp + quote, is often used too.
Here's Writing lambda expressions in common lisp (another StackOverflow question) which covers in-depth the particular case of (lambda <form>)
Note: *print-pretty* is NIL for these examples.
(defun where (x)
#'(lambda (item)
(> item x)))
In above function where you are creating an anonymous function and you are returning it as a closure (the function plus variable binding for X). Since you are returning it as a value, you have to write (FUNCTION (LAMBDA ...)). #'(lambda ...) is a notation which is shorter, but results in the same - using the reader macro #':
CL-USER 74 > (read-from-string "#'(lambda (foo) (1+ foo))")
(FUNCTION (LAMBDA (FOO) (1+ FOO)))
You can also write:
(defun where (x)
(lambda (item)
(> item x)))
During the definition of Common Lisp it has been added to be able to write above code. It is also identical to the (function (lambda ...)) form. In Common Lisp LAMBDA is macro, which expands into it:
CL-USER 75 > '(lambda (foo) (1+ foo))
(LAMBDA (FOO) (1+ FOO))
CL-USER 76 > (macroexpand '(lambda (foo) (1+ foo)))
(FUNCTION (LAMBDA (FOO) (1+ FOO)))
T
So, LAMBDA is a macro and when the evaluator sees it as in (lambda ...), it expands the form into a (function (lambda ...)) form, which then gets evaluated.
FUNCTION is a special form and when the evaluator sees it, it returns a function object - in the case of (function (lambda (foo) (1+ foo))) it returns the anonymous function as an object:
CL-USER 77 > (function (lambda (foo) (1+ foo)))
#<anonymous interpreted function 406000761C>
So you see that (function (lambda ...)) is the real s-expression notation to get a function object and both #'(lambda ...) (via a reader macro) or (lambda ...) (via a macro) are shorter notations in Lisp source code. It is unusual for a programmer to use the long form. Most (99.999%) use one of the shorter notations in source code.
Btw.: If the evaluator sees function enclosing a name of a function like this (function sin), then it looks up the function binding and returns the corresponding function object:
CL-USER 78 > (function sin)
#<Function SIN 4110083C6C>