freenode/#lisp - IRC Chatlog
Search
20:03:15
pfdietz
If you want a slot to have a default value even if not initialized, I suggest writing a method for slot-unbound.
20:03:46
pfdietz
I use that for slots that implement caches. When unbound, do the expensive computation, then cache the value in the slot.
20:58:32
jackdaniel
pfdietz: I've once heard you saying doing that and I've used this technique a few times since
21:30:32
Xach
I do that quite a bit and sometimes I forget how a slot is initialized. I should start writing describe-object methods that mention how a particular slot is populated.
21:39:44
anlsh
When I do (safe-square (incf *a*)), I expect it to return the (once-only (x) `(* ,x ,x)), and once-only returns '(+ 3 4) no matter what, so I expect (safe-square (incf *a*)) => (+ 3 4)
21:39:56
no-defun-allowed
If you're looking to bind x, then you need to create a let form around the body.
21:41:39
no-defun-allowed
Remember that macros function just like normal functions and are subject to normal evaluation rules, they just happen to output code and have slightly different lambda lists.
21:43:56
anlsh
See here's where I'm confused: lisp functions always return the result of evaluating their last form, correct? (aside from some when some special operators are used)
21:46:14
anlsh
so when evaluating the once-only, the result is `(+ 3 4), so I guess I'm wondering why that isn't just passed up
21:53:08
no-defun-allowed
Well yes, `(+ 3 4) is evaluated in the macro body to yield (+ 3 4), which is then evaluated by the code containing the macro call.
21:58:15
anlsh
I see, I guess not evaluating a macro post-expansions would just make their return values useless haha
22:00:14
anlsh
So am I correct that, aside from a few things like destructuring parameters lists and &body, the forms (labels ((op-name <some-definition>)) (eval (op-name args))) are (macrolet ((op-name <some-definition>)) (op-name args)) are pretty much equivalent?
22:01:14
no-defun-allowed
I'm not sure, macros can get some insight on the environment they're evaluated in. Also, the compiler can optimise macros better.
22:01:43
anlsh
I mean I guess that macros can do critical stuff like control evaluation order which you can't do with the function definition, but the point being the extra eval when written as function call
22:02:15
pjb
anlsh: For example: (defun foo (x) (return-from foo (+ 42 x)) 33) The last form is 33. But the result is the result of evaluating (+ 42 x).
22:06:12
pjb
anlsh: (+ 3 7) is never evaluated. The result of `(+ 3 4), which is (+ 3 4) is evaluated at run-time, after the macroexpansions have been done.
22:07:07
pjb
Now, in (defmacro once-only ((&rest names) &body body) `(+ 3 4)) (defmacro safe-square (x) (once-only (x) `(* ,x ,x)))
22:07:40
pjb
(once-only (x) `(* ,x ,x)) is expanded when the safe-square macro is compiled, and the returned expansion (+ 3 4) is evaluated when the safe-square macro itself is expanded.
22:08:41
pjb
(macroexpand '(once-only (x) `(* ,x ,x))) #| --> (+ 3 4) ; t |# (macroexpand '(safe-square (incf *x*))) #| --> 7 ; t |#
22:09:23
pjb
after the macroexpansion of the call to once-only, it's like if (defmacro safe-square (x) (+ 3 4)) had been written.
22:09:50
pjb
When expanding (safe-square (incf *x*)), (+ 3 4) is evaluated returning 7, and this is the expansion returned by safe-square.
22:10:56
anlsh
I see, realizing macroexpansion consists of both an expansion step and an evaluation step clears things up a lot
22:12:13
pjb
But the resulting expansion that is returned by the macro function is not evaluated AT ALL!
22:13:42
pjb
python476: not currently. Coarsely, I've seen job offers about once every 4 or 5 years, not more.
22:14:35
pjb
python476: the best is to start your own company, and use lisp to develop solutions for your customers, like dim does with his sql tools.
22:16:10
python476
maybe later, when I have a stable situation I'll do [common]lisp project in-house
22:17:06
pjb
For example, when I have to write code in C, I write it in CL first, debug it, and when it's good, I translate it to C.
22:17:43
pjb
Or since I use emacs, I may write emacs lisp code to help me produce the code in the programming languages I need to use.
22:18:46
python476
I sense that this dynamic prototyping for near optimal solution to optimized rewrite is becoming a skill to spread
22:18:47
pjb
python476: I recently improved my LINC translator: https://github.com/informatimago/lisp/tree/master/languages/linc which let me generate C code from sexps.
22:19:06
python476
read a few articles about people ditching straight c++ for python or else on first drafts
22:22:53
pjb
https://github.com/informatimago/lisp/blob/master/languages/linc/test-statements.sexpc generates https://pastebin.com/DWE8Yk4L
0:09:38
no-defun-allowed
Fortunately I don't think I need that kind of function now but it does sound handy to know.
3:43:10
beach
minion: Memo for jmercouris: Because it only removes from the end, whereas subseq can simultaneously remove from the start and from the end.