freenode/#lisp - IRC Chatlog
Search
2:26:41
elderK
The reason I ask, instead of just using symbols, is that for efficiency, I'd like to represent something as a 2D array. Say, a transition table.
2:38:52
conjunctive
Hi all, just released a Huet-style zippers library for CL: https://github.com/conjunctive/zip
2:39:05
conjunctive
Also, working with FSet has been fantastic! Coming from Clojure I felt right at home.
2:50:16
elderK
pjb: If using a case statement, would this require translating the integer back to a symbol, and such
3:00:07
adlai
elderK: if you want to roll-your-own, i recommend an eq table from symbols to numbers; anyone concerned about the backlinks deserves the algorithmic complexity.
3:01:00
pjb
elderK: defconstant defines constant variables. Theorically, they could be implemented as symbol-macros, but implementations define them natively, to be able to detect bindings at compilation time, and for keywords.
3:02:37
pjb
elderK: well, defining constant variables as symbol-macro wouldn't let you prevent binding them.
3:02:54
adlai
elderK: instead of a 2D array containing symbols designating the elements within the enum, (make-hash-table :test #'eq) for mapping symbols to offsets within the array; then, you don't even have to allocate that array if it's prohibitively large.
3:05:22
elderK
adlai: I'd rather avoid a hashlookup every time I need the integer value of some symbol.
3:05:44
elderK
I need to learn more about the case form. Are the things it matches against even evaluated?
3:06:09
pjb
elderK: you have to define boundaries, outside of which the symbols are used and inside of which the integers are used.
3:07:09
pjb
elderK: If you define constant variables, they have to be defined at compilation-time, so you can use them with #. in case forms processing the integer values.
3:07:37
pjb
In all cases, I don't see the point of using a hash-table, conversion functions can just use case.
3:14:26
adlai
elderK: if you'd like to reoptimize an eq hashtable once you're convinced that the enum's membership set has settled, you can verify that hash-table-count and hash-table-size are sufficiently far apart, and then call rehash; however, this is all an implementation detail, and your enum lookups should happen at compile-time
3:16:27
adlai
myeah, there's no cl:rehash, although the desired side-effect is to rebuild the hashtable, without another call to make-hash-table, so that optimizers who pay attention to the membership set have an opportunity to work
3:19:17
Bike
each case has a list of keys that are checked against with eql. if a clause key is T or OTHERWISE it's a default. if a clause key is some other object that counts as a list of one object. that's about it.
3:21:16
adlai
maybe elderK is just asking "how is case implemented, when i reserve the right to use as many distinct keys as the size of some mystery 2D program text"
3:22:05
Bike
it means it can be compiled into a jump table sometimes. that's nice. but it's fundamentally a pretty static operator.
3:23:23
adlai
elderK: you may find the string-case macro implements exactly what you didn't think you were asking, if you want the enum members to exist at runtime
3:25:42
Bike
so that's how cl:case works as well. C does have a more expansive concept of constant expressions, though.
3:27:10
adlai
there is a string-case library that builds a table from strings at compile time, and evaluates the key at run time, although it is not supposed to be abused as a replacement for enumerations
3:28:08
adlai
elderK: in your example, the symbol foo might never be evaluated after the compiler hits defconstant
3:29:08
Bike
the macro trying to evaluate the keys would be a problem because A) it needs a list, and B) case is actually used with literal unevaluated symbols a fair bit
3:29:20
Bike
might be interesting to think about an expansion of what constant expressions are good for, tho
3:29:47
elderK
:( So, what's the idiomatic equivalent to a C enumeration, and switching on enumerants? Or using those enumerants as indices?
3:31:18
elderK
So then, if I want to have a mapping between symbols and integer indices, I need a map of somesort.
3:32:00
Bike
yeah. well, just define a symbol->integer function that everything uses, and stress about the best way to implement it later
3:34:31
Bike
if the implementation evaluates defconstants at compile time, as most do, that works fine. if you wanna be paranoid you can put an eval-when on
3:57:59
aeth
elderK: The most idiomatic equivalent to an enum is a member type, e.g. (deftype color () '(member :red :green :blue)) (typep :red 'color)
3:58:45
aeth
Of course it doesn't actually have an (accessible) associated number until you define an ECASE that maps it to a number, as Bike said.