Eval is such a powerful function I know I will not think of all the cool things it can do.
the basic function is like this
(eval expression)
it has four basic modes (two that are trivial)
(eval number) returns that number
(eval function) returns that function
the two interesting cases are
(eval list) and (eval string)
if the expression is a list it will run that list as a called function
for example (eval (list "cat" 1 1)) will give you 11
so as you can see you can build your own code with this and run it
if the expression is a string it will treat that string as the name of the variable. (sorry it is hard to explain just think of this case as the opposite of set)
for example
(setq bob 11)
(setq myVar "bob")
(eval myVar)
the eval will return 11
beyond confusing people who look at your code what use is this?
well other than the storing code for later use you can store something that could be a global variable or a function. So say you have some data in a system some need a function called to find the data but some can just look at a global variable. With one simple line of code you can do both.
With this and apply you can have a class of things (like an item) where some have one function and others have different functions.
As I said earlier this is so powerful I know I am only scratching the surface if anyone else wants to add something please speak up or if you don't understand something please tell me so I can improve on this.
fun with eval
- Betelgeuse
- Fleet Officer
- Posts: 1920
- Joined: Sun Mar 05, 2006 6:31 am
Crying is not a proper retort!
- Periculi
- Fleet Officer
- Posts: 1282
- Joined: Sat Oct 13, 2007 7:48 pm
- Location: Necroposting in a forum near you
Interesting, I know I can use this.
If I have a function:
<Globals>
(block nil
(setq myFunction (lambda nil
(block (return)
(setq return (add 2 2))
)
))
)
</Globals>
and sometime I call eval like this:
(eval "myFunction")
I get 4 back?
What about if the function expects data? Is that what the list is used for?
(setq myFunction (lambda (a b)
(block (return)
(setq return (add a b))
)
))
and sometime I call eval like this:
(eval (list "myFunction" 2 2))
I get 4 back again?
If I have a function:
<Globals>
(block nil
(setq myFunction (lambda nil
(block (return)
(setq return (add 2 2))
)
))
)
</Globals>
and sometime I call eval like this:
(eval "myFunction")
I get 4 back?
What about if the function expects data? Is that what the list is used for?
(setq myFunction (lambda (a b)
(block (return)
(setq return (add a b))
)
))
and sometime I call eval like this:
(eval (list "myFunction" 2 2))
I get 4 back again?
- Betelgeuse
- Fleet Officer
- Posts: 1920
- Joined: Sun Mar 05, 2006 6:31 am
nope you get the function backand sometime I call eval like this:
(eval "myFunction")
I get 4 back?
it returns whatever is in the myFunction variable due to it being the string case
if you wanted the function run you would (eval (list "myFunction")) or using apply (apply myFunction nil) depending if you had the function itself (like you created it with lambda and passed it) or if you just had the name
yep you get the 4 back but remember that is due to it being a list and with lists it runs it as code.(setq myFunction (lambda (a b)
(block (return)
(setq return (add a b))
)
))
and sometime I call eval like this:
(eval (list "myFunction" 2 2))
I get 4 back again?
Crying is not a proper retort!