Just another encounter of newlisp flexibility ;-)

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Just another encounter of newlisp flexibility ;-)

Post 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)
>
-- (define? (Cornflakes))

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

(apply max b)

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Nice! You only have to know the trick.
;-)
And Sam gets it always shorter.
Hans-Peter

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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

PaipoJim
Posts: 20
Joined: Fri Jun 03, 2005 3:21 pm
Location: Oregon

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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

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

PaipoJim
Posts: 20
Joined: Fri Jun 03, 2005 3:21 pm
Location: Oregon

Post 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

-

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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

Locked