[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Returning the larger function

gengyangcai

11/15/2015 11:50:00 AM

I wrote a function that takes 2 arguments and returns the larger one.

CL-USER 4 : 2 > (defun greater (x y)
(if (> x y) x y))
GREATER

CL-USER 5 : 2 > (greater 5 6)
6

How do i extend it to 3 elements or more ?
6 Answers

rpw3

11/15/2015 12:05:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> wrote:
+---------------
| I wrote a function that takes 2 arguments and returns the larger one.
....
| How do i extend it to 3 elements or more ?
+---------------

Don't bother. Just use the builtin function MAX:

http://www.lispworks.com/documentation/HyperSpec/Body/f...

Example:

> (max 4 6 17 9 2)

17
>


-Rob

-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <http://rpw...
San Mateo, CA 94403

gengyangcai

11/15/2015 12:28:00 PM

0

sweet


On Sunday, November 15, 2015 at 8:10:07 PM UTC+8, Rob Warnock wrote:
> CAI GENGYANG <gengyangcai@gmail.com> wrote:
> +---------------
> | I wrote a function that takes 2 arguments and returns the larger one.
> ...
> | How do i extend it to 3 elements or more ?
> +---------------
>
> Don't bother. Just use the builtin function MAX:
>
> http://www.lispworks.com/documentation/HyperSpec/Body/f...
>
> Example:
>
> > (max 4 6 17 9 2)
>
> 17
> >
>
>
> -Rob
>
> -----
> Rob Warnock <rpw3@rpw3.org>
> 627 26th Avenue <http://rpw...
> San Mateo, CA 94403

Norbert_Paul

11/15/2015 1:14:00 PM

0

CAI GENGYANG wrote:
> I wrote a function that takes 2 arguments and returns the larger one.
....
> How do i extend it to 3 elements or more ?

(defun greatest (x &rest yz)
(reduce #'greater yz :initial-value x))
=> GREATEST

(greatest 7)
=> 7

(greatest 7 8 9 3)
=> 9

(greatest :foo)
=> :FOO

(greatest :foo 3)
-> Error


William James

11/15/2015 1:15:00 PM

0

CAI GENGYANG wrote:

> I wrote a function that takes 2 arguments and returns the larger one.
>
> CL-USER 4 : 2 > (defun greater (x y)
> (if (> x y) x y))
> GREATER
>
> CL-USER 5 : 2 > (greater 5 6)
> 6
>
> How do i extend it to 3 elements or more ?

MatzLisp (Ruby):

def greatest *args
args.max
end

greatest 5,9,2
==>9
greatest "eeny","mo","meeny"
==>"mo"

--
"It is not as wrong raping a Swedish girl as raping an Arab girl," says Hamid,
in an interview about a gang rape involving a Swedish girl and immigrant
perps.... [T]his massive spike in rapes is caused by mass immigration ... that
.... international organizations want to continue ... until the native
population has been reduced to a persecuted minority.
--- amren.com/news/2009/09/sweden_tops_eur/

Pascal J. Bourguignon

11/15/2015 8:15:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> I wrote a function that takes 2 arguments and returns the larger one.
>
> CL-USER 4 : 2 > (defun greater (x y)
> (if (> x y) x y))
> GREATER
>
> CL-USER 5 : 2 > (greater 5 6)
> 6
>
> How do i extend it to 3 elements or more ?

First you have to ask yourself how you want to pass those elements to
the function.

You have at least two choices:


- pass a list of the elements.

(greater '(1 2 3)) --> 3
(let ((a 1)(b 2)(c 3)(d 4))
(greater (list a b c d))) --> 4


- pass the elements directly to a n-ary function:

(let ((b 2))
(greater 1 b 3)) --> 3


In both cases you will get the elements in a parameter bound to the list
of the elements, but in the later case, to call the function when you
have a list of element, you need use APPLY:

(apply (function greater) list-of-real)

This case also has the limitation of CALL-ARGUMENTS-LIMIT.


A third choice is to do both, like APPLY itself:

(greater 1 2 3 (list 4 5 6)) --> 6

but this requires more mangling of the parameters at run-time.


We have:

(greater a b) == (if (< a b) b a)

Next, what is the greater of 3 or more elements?

(greater a b c) == (greater a (greater b c))
(greater a b c d) == (greater a (greater b (greater c d)))
(greater a b c d e) == (greater a (greater b (greater c (greater d e))))
â?¦

as you can see, there's pattern to it.

(greater a b) == (if (< a b) b a)
(greater a b â?¦) == (greater a (greater b â?¦))

if there's only two elements, you use your basic formula, otherwise, you
take the greater of the the first and of the greater the other elements.

We can also define two additionnal base cases:

(greater) == nil
(greater a) == a


(defun greater (&rest elements)
(cond
((null elements) nil)
((null (cdr elements)) (first elements))
((null (cddr elements)) (if (< (first elements) (second elements))
(second elements)
(first elements)))
(t (greater (first elements)
(apply (function greater) (rest elements))))))


(mapcar (lambda (arguments) (apply (function greater) arguments))
'(()
(1)
(1 2)
(2 1)
(1 2 3 4)
(4 3 2 1)))
--> (nil 1 2 2 4 4)


You can see how impractical using &rest is. Compare with:

(defun greater (elements)
(cond
((null elements) nil)
((null (cdr elements)) (first elements))
((null (cddr elements)) (if (< (first elements) (second elements))
(second elements)
(first elements)))
(t (greater (list (first elements) (greater (rest elements)))))))

(mapcar (function greater)
'(()
(1)
(1 2)
(2 1)
(1 2 3 4)
(4 3 2 1)))
--> (nil 1 2 2 4 4)


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Pascal J. Bourguignon

11/15/2015 8:19:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> I wrote a function that takes 2 arguments and returns the larger one.
>
> CL-USER 4 : 2 > (defun greater (x y)
> (if (> x y) x y))
> GREATER
>
> CL-USER 5 : 2 > (greater 5 6)
> 6
>
> How do i extend it to 3 elements or more ?


Notice that poor CAI has no more memory than a month-long goldfish.
https://groups.google.com/d/msg/comp.lang.lisp/-U89Xv8U_Qs/98...

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk