Задача: Определить функцию, генерирующую набор функций от n до m подобных лисповой 1+.
Например: функция 8+ принимает 1000 и возвращает 1008.
Варианты Common Lisp:
(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