freenode/#lisp - IRC Chatlog
Search
8:51:39
zerobaud
Does anybody have a good overview / tutorial showing how metaprograming can be done in lisp? For example in C you want a enum of errors that indexes an array of strings (char arrays) corresponding to the error message so that; res = do_something(); if res != ERR_NONE { strerror(res);
8:53:11
zerobaud
There are better examples, but anyways... any good documentation about metaprogramming?
8:58:00
zerobaud
shrdlu68: sorry, I left information out.. I want a single place to add a error message and automatically add a enum. I can keep a enum with error names like ERROR_NONE, ERROR_THIS, then create a char array pointer to a char array with "strings" corresponding to the errors.
8:58:40
zerobaud
X macros allow you to add them in one place only trough metaprogramming (using the preprocessor)
9:01:50
zerobaud
shrdlu68: or for example, I want to call a error handling function strerror for libc, and also want to handle libelf erros with elf_errmsg, and errors from intel xed dissasembler using xed_error_enum_t2str.
9:03:04
zerobaud
Now, with metaprogramming you can call the correct function without having to return multiple values (orignal return, and a value indicating what lib caused the error)
9:03:51
zerobaud
so these are practical examples where C really cant do what you want, my question is there a list or tutorial detailing practical examples like those which lisp solves?
9:08:41
shrdlu68
Sorry, I still don't quite understand the problem, and how whether it has an analogous problem in CL.
9:09:43
zerobaud
shrdlu68: I can show a snippet of C not sure if you are familiar with it... I know this is a lisp channel, but if you are I think you will understand. I find it difficult to explain with just words.
9:10:16
schweers
zerobaud: what you seems to me more like memory management and maybe dispatch (i.e. OO) than metaprogramming.
9:12:04
shrdlu68
zerobaud: As I understand it, you can have a function that checks the value of "errno" and returns the corresponding string. That, or a macro that copy-pastes the code inline. Right?
9:12:38
zerobaud
schweers: #define LIST_OF_VARIABLES \ X(value1) \ X(value2) \ X(value3) void print_variables() { #define X(name) printf("%s = %d\n", #name, name); LIST_OF_VARIABLES #undef X }
9:13:48
zerobaud
scymtym: right, but X macros are frowned upon, ugly, and really cant do everything you want...
9:14:34
zerobaud
now I want to learn lisp to see if the afromentioned problems (for which I can provide snippets showing its impossible) can be solved in lisp
9:14:54
scymtym
zerobaud: sure, i just thought that page might help people understand where you are coming from with your question and the example
9:15:23
zerobaud
scymtym: yes thanks, I was about to post it as well since that one line is horrible to read ^_^
9:16:26
flip214
zerobaud: I guess you should do (DEFPARAMETER ERROR_NONE "no error") (DEFPARAMETER ERROR_THIS "that") etc. and just use the symbol name as identifier, doing (SYMBOL-VALUE error) to get the string.
9:17:03
flip214
or do you really need some enumeration via numeric IDs for outside communication? In that case I'd recommend a DEFERROR macro that does DEFPARAMETER and registers the error in an array.
9:17:19
schweers
scymtym: thanks for the link to that trainwreck, now I wont’t be able to sleep for two days ;-P
9:17:22
scymtym
zerobaud: in lisp, you can run arbitrary lisp code at compile time to generate code to be compiled. there is no separate text-based preprocessor
9:18:30
zerobaud
scymtym: so can I get the names of variables also, and construct code to execute based on them?
9:18:33
schweers
zerobaud: in lisp identifiers are first class values, called symbols. Thus, one can have a list of variable names or function names, or whatnot.
9:19:29
scymtym
zerobaud: not sure what you mean but variables, but yes, macros often generate code based on input that is also code
9:19:39
zerobaud
schweers: how would I get the variable name given the variable name? like what is the syntax like?
9:19:51
zerobaud
sorry I am really new to lisp, but its metaprogramming that makes me curious to learn it
9:21:22
schweers
I highly recommend you read Practical Common Lisp by Peter Seibel (http://gigamonkeys.com/book/), it is a decent into to Common Lisp in general, has a chapter on macros and has practical examples.
9:22:48
schweers
Using symbols as variable (or function, or class, or whatever) names can be a bit confusing at first, as you have to bind them to a variable in the macro in order to reference it. It took me quite some time to wrap my head around that.
9:23:23
zerobaud
schweers: I am afraid this is something you learn once you actually understand lisp decently
9:27:57
schweers
How do you guys use CALL-NEXT-METHOD? It’s not always obvious whether or not there will be another method to call. Do you always call it like this: (when (next-method-p) (call-next-method))?
9:30:57
no-defun-allowed
i call it anyway, since if there is nowhere to go it'll signal an error and that's usually what i want
9:31:29
no-defun-allowed
i suppose if you actually want the value though, eeeeh, that's probably the best solution if method combinations don't work
9:32:30
schweers
Sometimes I define a method on a class which derives from the default (STANDARD-OBJECT?), but due to MI there might be more methods to call.
9:33:37
flip214
schweers: you can use the APPEND method combination to run all methods and get a list of their results; or the + m.c.; etc.
9:33:54
no-defun-allowed
i don't know what you intend to do with the value, but if you're combining values with something like + or append, you can define a :method-combination in defgeneric and the method machinery will reduce the values using the combination
9:34:24
no-defun-allowed
http://gigamonkeys.com/book/object-reorientation-generic-functions.html#method-combination
9:36:52
schweers
Ahh, progn is predefined ... that may be what I want, I’ll take a closer look at it and what I actually need. Thanks for the input!
9:42:35
beach
I use CALL-NEXT-METHOD when I want to sometimes avoid calling the primary method(s). So I might have (if ... (call-next-method) (do-something-else))
9:44:34
schweers
beach: so do I. Sometimes I don’t call it at all (for instance when building a mock/fake/etc)
9:44:47
beach
Indeed, if you call CALL-NEXT-METHOD unconditionally, it is likely that there is a method combination for you, or that you might want to do it with a custom method combination.
10:06:08
hjudt
is it bad practice to load a foreign library when (quick-)loading the system and fail ungracefully if it doesn't exist? some systems seem to load the lib only on first usage (so it fails later).
10:25:05
schweers
hjudt: why fail ungracefully? I have seen systems which offer a restart to choose a different file location.
10:47:50
hjudt
another question: sbcl provides a synchronized hash-table. is there something similar for other implementations, or some "trivial-" package?
13:18:24
jmercouris
instead of making them part of the keyword package, but any other ideas? anything better I could be doing?
13:21:16
jmercouris
instead of making things part of the keyword package though, I really did enjoy how it looked when everything was prefixed by a ":"
13:24:31
jmercouris
I'm assuming sxql is downcasing all symbols from the keyword package or something, I don't know what wizardry it is doing
13:25:35
jmercouris
however, Sxql produces SQL queries with "fish" instead of "FISH" when you specify :fish
13:28:23
jmercouris
Yeah, I could make it so that my package is case sensitive by using named-readtables, basically right?
13:31:26
shka_
jmercouris: define-constant is nice because it allows you to pass your own test, but here you don't need it