Page 1 of 1

Just another encounter of newlisp flexibility ;-)

Posted: Thu Nov 03, 2005 9:17 pm
by newdep
'mod 'mul 'min 'max dont work by default on lists and in this case
I could not find a fast way to create a nice lambda for it.

This might look quiet simple thought its not someting you think of all the time. But it is fully newlisp.

So how to get the highest number out of a list of numbers?

> b
(65 42 48 32 25 46 32 14 43 77 15 136 2 3549 268 215 119 217 110
84 56 211 98 22 24 96 46 35 51 278 74 36 41 91 64 65 99 83 82 44
46 62 52 15 53 157 85 54 29 63 75 75 29 44 27 1 8 32 17 57 23 121
35 51 34)
>

> (push max b 0)
max <8052D00>
> (eval b)
3549 <--- tataaa there it is!

> b
(max <8052D00> 65 42 48 32 25 46 32 14 43 77 15 136 2 3549 268 215
119 217 110 84 56 211 98 22 24 96 46 35 51 278 74 36 41 91 64 65
99 83 82 44 46 62 52 15 53 157 85 54 29 63 75 75 29 44 27 1 8 32
17 57 23 121 35 51 34)
> (pop b 0)
max <8052D00>

> b
(65 42 48 32 25 46 32 14 43 77 15 136 2 3549 268 215 119 217 110
84 56 211 98 22 24 96 46 35 51 278 74 36 41 91 64 65 99 83 82 44
46 62 52 15 53 157 85 54 29 63 75 75 29 44 27 1 8 32 17 57 23 121
35 51 34)
>

Posted: Thu Nov 03, 2005 9:46 pm
by Sammo
(apply max b)

Posted: Thu Nov 03, 2005 9:47 pm
by HPW
Nice! You only have to know the trick.
;-)
And Sam gets it always shorter.

Posted: Fri Nov 04, 2005 12:29 pm
by newdep
hehe...the answer was right under my nose ;-)

Posted: Fri Nov 04, 2005 7:10 pm
by PaipoJim
HPW wrote: ...shorter.
But not neccesarily faster:

Code: Select all

> (setq c (flat (dup b 1000)))
....35 51 34

> (time (apply max c))
49

> (time ((push max c) (eval c)))
23

> (pop c)
max <C42C> 

> (time (apply max c))
48 

I'm wondering if pushing a function onto a list is always faster than mapping apply or is it just a peculiarity of the max function with a large list?
-

Posted: Fri Nov 04, 2005 8:50 pm
by newdep
You have a point there (Not that I mind the milliseconds but its nice to know) Actualy I would like to add to that ->

Is 'max quicker on a sorted or unsorted list ;-)

Regards, Norman.

Posted: Fri Nov 04, 2005 9:56 pm
by PaipoJim
newdep wrote: Is 'max quicker on a sorted or unsorted list ;-)
Ha ha, very funny! One thing for sure, sort is a really slow way to get the max:

Code: Select all

> (time (sort c))
183

> (last c)
3549

FWIW, I can't really see much difference in max with sorted input:

Code: Select all

> (time ((push max c) (eval c)))
27

-

Posted: Fri Nov 04, 2005 10:19 pm
by newdep
Im glad you where awake ;-)