Re: letrec/named let

*To*: mathgroup at smc.vnet.net*Subject*: [mg56740] Re: [mg56707] letrec/named let*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Thu, 5 May 2005 06:01:22 -0400 (EDT)*References*: <200505040433.AAA06220@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On 4 May 2005, at 13:33, Daniel Roy 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 > Unfortunately I am out of practice with Lisp. Let, I think, corresponds to Mathematica's With. I don't think there is anything that corresponds to named let or letrec but they are not needed. The reason is that the similarity between Mathematica and Lisp is very deceptive. Mathematica's syntax, or more correctly one part of its syntax does indeed resemble Lisp but the "internals" of the two languages are quite different. (The most important difference is that Mathematica's lists are arrays and Lisp style linked lists have to be explicitly formed and are not very easy to use.) You can program (with some effort) in Mathematica in Lisp style just as you can program in C style, but if you do so your programs will usually be not very efficient and in some cases unmanageably inefficient. I know because many years ago when I started to program in Mathematica I also attempted to program in Lisp style. Actually your program CompressNumericalSequence performs better than I would have expected but almost any program written in a more natural Mathematica style will outperform it. Here is a very casual attempt: CompressNumericalSequence1[s_] := First[NestWhile[With[{a = First[#], b = Last[#]}, If[FreeQ[a, b], {a /. x_ /; x > b :> x - 1, b}, {a, b + 1}]] &, {s, 1}, Last[#] < Max[First[#]] &]] test=Table[Random[Integer,20],{10^6}]; ans1=CompressNumericalSequence1[test];//Timing {3.84 Second,Null} ans2=CompressNumericalSequence[test];//Timing {11.66 Second,Null} In[6]:= ans1==ans2 True Andrzej Kozlowski Chiba, Japan http://www.akikoz.net/andrzej/index.html http://www.mimuw.edu.pl/~akoz/

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