Is there a way in lisp-family (EDIT: lisp-1) languages to differentiate symbol evaluation with regard to its position as as function or as an argument (i.e. override eval of this symbol in regard to when it is evaluated)?
As an example (I don't need this functionality, this is an example), I wanted to implement some sort of infix operation on a set of objects, which can be called by an object itself
(my-obj some-operator arg1 ...)
which will actually apply function some-operator to my-obj and arguments.
But when this object is used anywhere else in code as an argument, like:
(some-function my-obj &args...)
it will evaluate to a value of my-obj.
Thank you.
In Racket it is possible to do a couple things in this spirit:
You can define a struct
and give it a prop:procedure
. When an instance of the struct
is supplied in an application, the procedure will be called.
You can override the default #%app
with your own function, to redefine application generally, and including things that aren't struct
s. For example you can do things like emulate Clojure's (key map)
syntax, so that ('symbol dict)
is actually (hash-ref dict 'symbol)
.