Answer the question
In order to leave comments, you need to log in
How to define a function that generates a set of functions from n to m similar to Lisp 1+?
Task: Define a function that generates a set of functions from n to m similar to Lisp 1+.
For example: function 8+ takes 1000 and returns 1008.
Common Lisp variants:
(defun make-function-number-plus (n m)
(loop for a from n to m do
(make-function (add-plus a) '(z) `(+ z ,a))))
(defun add-plus (a)
(read-from-string
(concatenate 'string (write-to-string a) "+")))
(defun make-function (name prms body)
(eval `(defun ,name ,prms ,body)))
> (make-function-number-plus 2 10)
NIL
> (7+ 1000)
1007
; автор – helter, www.cyberforum.ru
(defun make-function-number-plus (n m)
(loop for a from n to m do
(let ((a a))
(setf (symbol-function (add-plus a))
(lambda (z) (+ z a))))))
(defun add-plus (a)
(intern (format nil "~A+" a)))
> (make-function-number-plus 2 10)
NIL
> (7+ 1000)
1007
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question