Re: letrec/named let

*To*: mathgroup at smc.vnet.net*Subject*: [mg56777] Re: [mg56707] letrec/named let*From*: DrBob <drbob at bigfoot.com>*Date*: Thu, 5 May 2005 06:04:37 -0400 (EDT)*References*: <200505040433.AAA06220@smc.vnet.net> <opsp9mdot7iz9bcq@monster.ma.dl.cox.net> <1115235138.31794.120.camel@30-5-214.wireless.csail.mit.edu> <opsp9n4xswiz9bcq@monster.ma.dl.cox.net> <1115245251.31821.123.camel@30-5-214.wireless.csail.mit.edu>*Reply-to*: drbob at bigfoot.com*Sender*: owner-wri-mathgroup at wolfram.com

Call the previous solution "noDispatch"; the "dispatch" solution will be much faster for large lists: Clear[noDispatch, dispatch] noDispatch[s_List] := Module[{u = Union@s}, s /. Thread[u -> Range@Length@u]] dispatch[s_List] := Module[{u = Union@s}, s /. Dispatch@Thread[u -> Range@Length@u]] data=Table[Random[],{1000}]; Timing[one=dispatch@data;] Timing[two=noDispatch@data;] one==two {0.016 Second,Null} {0.468 Second,Null} True That's almost 30 times faster, but double the size of the list, and now it's 122 times faster: data=Table[Random[],{2000}]; Timing[one=dispatch@data;] Timing[two=noDispatch@data;] one==two {0.016 Second,Null} {1.953 Second,Null} True With 3000 elements, the speed ratio is 300 to 1, so Dispatch is well worth the trouble. I have a feeling there might be a faster method using a variant of UnsortedUnion -- see the examples in Help for Union -- but it's just a feeling, so far. Bobby On Wed, 04 May 2005 18:20:51 -0400, Daniel Roy <droy at MIT.EDU> wrote: > Ahah.. This works. Very good solution! (much faster than my solution > whose performance is dependent on the absolute size of the values ! > ugly) > > dan > > On Wed, 2005-05-04 at 14:51 -0500, DrBob wrote: >> Clear@CompressNumericalSequence >> CompressNumericalSequence[s_List] := Module[{u = Union@s}, >> s /. Thread[u -> Range@Length@u] >> ] >> CompressNumericalSequence@{5, 1, 1, 2, 3, 4} >> >> {5,1,1,2,3,4} >> >> Bobby >> >> On Wed, 04 May 2005 15:32:18 -0400, Daniel Roy <droy at MIT.EDU> wrote: >> >> > I just figured out why the suggestions I've been given fail on my test >> > cases. Its because the sets that I am passing can have repeated digits. >> > >> > CompressNumericalSequence@{10,2,2,4,7,8} >> > {5,1,1,2,3,4} >> > >> > Otherwise, your solution of Ordering@Ordering is MUCH faster. >> > Unfortuantely, it gives the wrong answer on the above test. >> > >> > -dan >> > >> > >> > On Wed, 2005-05-04 at 14:14 -0500, DrBob wrote: >> >> I have no idea what "named let" does -- not a hell of a lot, I suspect -- but here's a simple replacement for the other routine: >> >> >> >> CompressNumericalSequence[s_List]:=Ordering@Ordering@s >> >> CompressNumericalSequence@{10,2,4,7,8} >> >> >> >> {5,1,2,3,4} >> >> >> >> Bobby >> >> >> >> On Wed, 4 May 2005 00:33:53 -0400 (EDT), Daniel Roy <droy at mit.edu> wrote: >> >> >> >> > hi. i'm a lisper/schemer and i'm working with mathematica. i >> >> > appreciate the lisp-like nature of mathematica but i can't seem to >> >> > easily replicate some of the functionality i like which is forcing me to >> >> > write ugly side-effect code. >> >> > >> >> > for instance, how do you do the equivalent of a "named let" in >> >> > mathematica (NOTE! I know i can take the max of a list, this is just a >> >> > simple example of a named let) >> >> > >> >> > (define (max-of-list lst) >> >> > (let loop ((lst (cdr lst)) >> >> > (best (car lst))) >> >> > (if (null? lst) >> >> > best >> >> > (loop (cdr lst) >> >> > (if (> (car lst) best) >> >> > (car lst) >> >> > best))))) >> >> >(max-of-list '(1 2 3 4 5 2)) >> >> >> 5 >> >> > >> >> > Here is a mathematica function to compress a sequence numerically. >> >> > here is one attempt using functions where i pass the function to >> >> > itself... there has to be a better way >> >> > >> >> > CompressNumericalSequence[S_] := Module[ >> >> > {C = Function[{C, R, i}, >> >> > If[i < Max[R], >> >> > If[Length[Position[R, i]] == 0, >> >> > C[C, (If[# > i, # - 1, #]) & /@ R, i], >> >> > C[C, R, i + 1]], >> >> > R]]}, >> >> > C[C, S, 1]]; >> >> > >> >> > CompressNumericalSequence[{10, 2, 4, 7, 8}] >> >> > {5, 1, 2, 3, 4} >> >> > >> >> > Also, is it possible to do letrec in mathematica? (essentially, i know >> >> > i can do recursive function declarations at the top level... my question >> >> > is whether i can do them at lower levels?)... >> >> > >> >> > thanks, dan >> >> > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> > >> >> >> >> >> >> >> > >> > >> > >> > >> >> >> > > > > -- DrBob at bigfoot.com

**References**:**letrec/named let***From:*Daniel Roy <droy@mit.edu>