I am trying to make a function call using the data I read from an input file. I have the following function that takes a list of vertices and an arbitrary numbers of commands. (I simplified the function a bit here).
(create-graph vertex-list cmd0 cmd1 ... cmdn)
My input file would have something like:
(2
(vertex-create "Paris")
(vertex-create "London"))
where 2 is the the number of vertices.
With that input, I would like to call create-graph as
(create-graph
'(v1 v2)
(set! v1 (vertex-create "Paris"))
(set! v2 (vertex-create "London"))
)
where v1 and v2 are symbols created for each vertex.
What I have now is to construct the whole function as a string and use eval-string. But is there a way to make that function call without using eval-string? Using eval for some parts should be fine. More specifically, I think I need to be able to do these dynamically:
- create a list of symbols, and
- create the commands (e.g. (set! v1 (vertex-create “Paris”)))
I would appreciate your input. Thanks!
Yes, you can do this without eval. Yes, that’s a much better idea. It’s important to understand that every program can be seen as an interpreter for its inputs, but once you understand eval, you should never use it again….
Your program might wind up looking something like this:
I assumed that rather than specifying the number of vertices, that you’d just infer it from the length of the list of commands. Also, I can’t see what other “commands” there might be. Also,
your set!s are unnecessary and yucky :).