Skip to content

Instantly share code, notes, and snippets.

@grzs
Created November 24, 2023 22:46
Show Gist options
  • Save grzs/06adce9d977a6b51f47b459138b45d9e to your computer and use it in GitHub Desktop.
Save grzs/06adce9d977a6b51f47b459138b45d9e to your computer and use it in GitHub Desktop.
How to pass functions as parameters in lisp
;; definitions
(defun simple-handler (f args)
(if (and (functionp f) (listp args))
(apply f args)))
(defun apply-handler (X)
(if (and (symbolp X) (fboundp X) (boundp X))
(apply (symbol-function X) (symbol-value X))))
(defun apply-cons-handler (X)
(if (and (consp X) (fboundp (car X)))
(apply (symbol-function (car X)) (cdr X))))
;; applications
(simple-handler (symbol-function '+) '(7 9))
(let ((a '(2 3)))
(fset 'a (symbol-function '+))
(apply-handler 'a))
(let ((b '(t)))
(setcar b '+)
(setcdr b '(5 7))
(apply-cons-handler b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment