Re: letrec/named let

*To*: mathgroup at smc.vnet.net*Subject*: [mg56769] Re: [mg56707] letrec/named let*From*: DrBob <drbob at bigfoot.com>*Date*: Thu, 5 May 2005 06:03:21 -0400 (EDT)*References*: <200505040433.AAA06220@smc.vnet.net>*Reply-to*: drbob at bigfoot.com*Sender*: owner-wri-mathgroup at wolfram.com

Just to clarify, each definition should be preceded by Clear[max]. Here are THREE ways to compute the maximum. Possibly some of them are in the same spirit as your "named let" solution. Clear@max max[s:{first_,others___}]:=max[first,{others}] max[best_,{}]:=best max[best_,rest:{next_,others___}]/;best>next:=max[best,{others}] max[best_,rest:{next_,others___}]:=max[next,{others}] max@{1,2,3,4,5,2} 5 max=Fold[Max,First@#,Rest@#]&; max@{1,2,3,4,5,2} 5 max=Fold[Max,-Infinity,#]&; max@{1,2,3,4,5,2} 5 The built-in Max can be replaced by If[#1>#2,#1,#2]& in the last two solutions. 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>