Sujet : Re: Math operation abstraction
De : spibou (at) *nospam* gmail.com (Spiros Bousbouras)
Groupes : comp.lang.lispDate : 28. May 2024, 23:10:11
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <OEX3U1SVrO3XtgFC3@bongo-ra.co>
References : 1
On Mon, 27 May 2024 23:29:56 +0100
Daniel Cerqueira <
dan.list@lispclub.com> wrote:
Hi.
I have this function
```
(defun muo (func a b)
(cond
((eq b 1) a)
(t (funcall func a (muo func a (1- b))))))
```
Which I can use for obtaining the multiplication operation using the
addition. As an example:
* (muo #'+ 5 2)
10
and "muo #'+" means "use the multiplication".
Now if I want the exponentiation I have to do:
* (muo #'(lambda (a b) (muo #'+ a b)) 5 2)
25
This creates the exponentiation.
I would like to obtain the exponentiation using "(muo #'+ 5 2 2)"
instead, being the last argument ("2") the number of times the operation
is applied. In the case of exponentiation, it applies the multiplication
2 times, resulting in the exponentiation math operation.
I am hoping I am explaining this concept well enough.
Basically, "muo #'+" creates the addition twice, which is the
multiplication, and "muo #'+ a b 2" should create the multiplication
twice, which is the exponentiation. If I use "muo #'+ a b 3" it should
create the exponentiation twice, which is the exponentiation of the
exponentiation.
I need help in making this code modification. Seeing code is what I
want. If you can write the code and explain it, better.
You don't give a precise definition but perhaps the following gives you
what you want or at least a starting point :
(defun muo-aux (func a b)
(cond
((eql b 1) a)
(t (funcall func a (muo-aux func a (1- b))))))
(defun muo (func a b repeats)
(cond
((eql repeats 1)
(muo-aux func a b))
((< 1 repeats)
(muo-aux (lambda (x y) (muo func x y (1- repeats))) a b))))
-- vlaho.ninja/menu