1

I realize that a function can be referenced using #'PRINT to reference the PRINT function. If we have a list S where the first element is 'PRINT , can we reference this using #(car S) ?

Say I'm looking at a list where the elements in the list are atoms which are the names of some functions. Currently, I can do this:

(defun aritheval (S)
    (funcall
        (cond
            ((eq '+ (car S)) #'+)
            ((eq '- (car S)) #'-)
            ((eq '* (car S)) #'*)
            ((eq '/ (car S)) #'/)
        )
        '2
        '3
    )
)

This function would compute 2+3, 2-3, 2*3 or 2/3 depending on the first element in list S. S contains the names of these functions.

1 Answer 1

6

#(car S) makes no sense. The syntax exists but means a vector with symbols CAR and S.

use

(funcall (first somelist) someargument)

or

(apply (first somelist) a-list-of-arguments)

Your function is non-Lispy formatted.

Trailing parentheses are not used in proper Lisp code. You also don't need to quote numbers. Numbers are self-evaluating, they evaluate to themselves. Also we now may prefer FIRST over CAR and REST over CDR. The functions do the same, but the names are better. Whenever we deal with simple lists, FIRST, SECOND, THIRD, ... and REST are used.

(defun aritheval (S)
  (funcall (cond ((eq '+ (car S)) #'+)
                 ((eq '- (car S)) #'-)
                 ((eq '* (car S)) #'*)
                 ((eq '/ (car S)) #'/))
            2 3)))

Since symbols can be used as names for global functions, above is not necessary.

This function below does the same, given the mapping from symbol to function is the same.

(defun aritheval (s)
  (funcall (first s) 2 3)))
Sign up to request clarification or add additional context in comments.

2 Comments

How do I reference a function where the name of the function is stored in a list? Say the list S has been defined by (setq S '(PRINT RESTORE))
Both apply and funcall take a function designator, so symbols that have function definitions associated with them are valid arguments. Also, setq doesn't define anything, it only set existing bindings. Use defparameter or defvar to define global variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.