Re: letrec/named let
- To: mathgroup at smc.vnet.net
- Subject: [mg56753] Re: letrec/named let
- From: Daniel Roy <droy at mit.edu>
- Date: Thu, 5 May 2005 06:01:34 -0400 (EDT)
- Organization: MIT
- Sender: owner-wri-mathgroup at wolfram.com
> You always give C as first argument; so it is superflous.
> Pleas note, that C is reserved:
> "C[i] is the default form for the ith parameter or constant
> generated in representing the results of various symbolic
> computations."
>
> CompressNumericalSequence[S_]:=
> Module[{fC},
> fC=Function[{R,i},
> If[i<Max[R],
> If[Length[Position[R,i]]==0,
> fC[(If[#>i,#-1,#])&/@R,i],
> fC[R,i+1]],R]];
> fC[S,1]];
>
> but this will fail for big numbers unless you set $IterationLimit
> appropriate. Hence,
I'm not so sure this is true. This function is tail-recursive. Does
mathematica handle tail recursion properly?
>
> CNS[lst_] := lst /. (Thread[#1 -> Ordering[Ordering[#1]]] & )[
> Module[{f}, f[x_] := (f[x] = Sequence[]; x); f /@ lst]];
>
> or even better
>
> CNS[lst_] := Module[{tmp = First /@ Split[Sort[lst]]},
> Flatten[(Position[tmp, #1] & ) /@ lst]]
>
> would do the task more reliable (and much more faster).
Neither of these functions do what CompressNumericalSequence does... And
because I'm no mathematica-guru I don't yet quite understand what they
do, anyway. Furthermore, they are actually SLOWER than my code (see
below on a test of 1,000,000 entries).
> > 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?)...
> Regards,
> Peter
dan