Hi /prague/
I'm programming in C and writing a little interactive program.
What's the best way to execute a function / piece of code depending on a string ? I could write a bunch of }else if(!strcmp(s, foo)){ but I find this hideous.
Any suggestions ?
Branching with !strcmp() is only efficient if there aren't many potential strings to compare. You should also only ever use this in situations requiring user input. Sage for non-Lisp.
Name:
Anonymous2012-04-19 13:27
It requires user input.
Also, it isn't /lisp/, it's /prog/.
Now simply walk down the tree by indexing the structs children with the first char of the string, stop when you reach a NULL and compare the rest of the input with the rest-field of the node.
So, rest and handler should be set only on leaf nodes. I guess the top of your mapping would be a node without an index.
Name:
Anonymous2012-04-19 15:02
>>9
insertion is left as an exercise for the reader.
Name:
Anonymous2012-04-19 16:07
A delicious trie, using a string as key to lookup a function pointer to the handler for that string
Check em<---
Name:
Anonymous2012-04-19 22:20
>>1
So you basically want a script compiler/interpreter or what?