On Wed, Sep 7, 2016 at 11:54 AM, Sanjeev Sharma <throw...@gmail.com> wrote:
> Just illustrates the changing structure (increased nesting depth) on each > recursive call > > The initial call to t2 changes the structure of the <rest> > argument/parameter - it puts in a list where there was no list. > > Each recursive call from inside t2 again changes the structure, adding an > enclosing '() for the <rest> parameter > > For the usual (no <rest>) function call It's not an issue for the standard > car/cdr idiom of walking down recursive structures. > > Okay, I think I follow you. My first piece of advice (which I think I already mentioned) is to use a fixed-arity helper function. I understand that you want to do this without a helper function, but, really, using a helper function is the idiomatic approach. That said, I'll provide an example of a recursive variable-arity function that uses `apply` instead. The purpose of `apply` is to use the individual members of a list as separate arguments to a function call. Here's a vararg version of `foldl`, limited to a single input "list": ``` #lang racket/base (require racket/match) (define (foldl* proc init . xs) (match xs ['() init] [(cons x xs) (apply foldl* proc (proc x init) xs)])) ``` In the recursive case, `apply` is used to provide the elements of xs as separate arguments to `foldl*`. Note that this function still generates a list as output. I suppose it could instead generate as many values as there are initial elements of xs, but that would be an awful pain to use. - Jon -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.