Just another encounter of newlisp flexibility ;-)

Started by newdep, November 03, 2005, 01:17:19 PM

Previous topic - Next topic

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)

>
-- (define? (Cornflakes))

Sammo

#1
(apply max b)

HPW

#2
Nice! You only have to know the trick.

;-)

And Sam gets it always shorter.
Hans-Peter

newdep

#3
hehe...the answer was right under my nose ;-)
-- (define? (Cornflakes))

PaipoJim

#4
Quote from: "HPW"
...shorter.


But not neccesarily faster:



> (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?

-

newdep

#5
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.
-- (define? (Cornflakes))

PaipoJim

#6
Quote from: "newdep"
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:



> (time (sort c))
183

> (last c)
3549



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



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


-

newdep

#7
Im glad you where awake ;-)
-- (define? (Cornflakes))