Created
November 24, 2023 22:46
-
-
Save grzs/06adce9d977a6b51f47b459138b45d9e to your computer and use it in GitHub Desktop.
How to pass functions as parameters in lisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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