Boo
Boo

Reputation: 1644

Set function to symbol

I have some problems with setting function to symbol. For example, I add some properties to symbol:

(putprop 'rectangle '10 'width)
(purprop 'rectangle '2 'height)
(putprop 'rectangle (* (get 'rectangle 'width) (get 'rectangle 'height)) 'square)

And when I type (symbol-plist 'rectangle) I get:

(SQUARE 20 HEIGHT 2 WIDTH 10)

But if I change height or value I get old square value:

(putprop 'rectangle 10 'height)
(symbol-plist 'rectangle)

How I can set function as symbol property? If I set property as lambda, symbol-plist gets me something like #<Closure-square: #12345>

Upvotes: 1

Views: 254

Answers (2)

aerique
aerique

Reputation: 981

I use symbol-function to define aliases for functions with good but long names:

* (defun hello-world () (format t "Hello, World!~%"))
HELLO-WORLD
* (hello-world)
Hello, World!
NIL
* (setf (symbol-function 'hw) #'hello-world)
#<FUNCTION HELLO-WORLD>
* (hw)
Hello, World!
NIL

This is a function I have created for this:

(defun defalias (function alias)
  "Defines an alias for FUNCTION, so it can be called with ALIAS as well."
  (setf (symbol-function alias) function))

Upvotes: 2

Dario Hamidi
Dario Hamidi

Reputation: 88

Setting the symbol's property to a lambda expression, does not automatically apply the that lambda expression every time other properties change (which is what I guess you want).

You could write a wrapper function that sets the symbol's height or width property and recalculates the the symbols square property.

Upvotes: 0

Related Questions