Re: [racket-users] Re: lambda and the equivalent define / "defun"
On Wed, Sep 7, 2016 at 12:18 PM, Jon Zeppieriwrote: > > 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, > Uh, and you can disregard this statement altogether, since it's crazy [sorry]. `foldl*`, like `foldl`, does not need to generate a list as output. -- 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.
Re: [racket-users] Re: lambda and the equivalent define / "defun"
On Wed, Sep 7, 2016 at 12:18 PM, Jon Zeppieriwrote: > > > ``` > #lang racket/base > > (require racket/match) > > (define (foldl* proc init . xs) > (match xs > ['() init] > [(cons x xs) (apply foldl* proc (proc x init) xs)])) > ``` > > I should provide an example. So, if you were using the normal `foldl`, you'd have: ``` > (foldl cons '() '(1 2 3 4)) '(4 3 2 1) ``` Instead, here's you'd do: ``` > (foldl* cons '() 1 2 3 4) '(4 3 2 1) ``` -- 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.
Re: [racket-users] Re: lambda and the equivalent define / "defun"
On Wed, Sep 7, 2016 at 11:54 AM, Sanjeev Sharmawrote: > Just illustrates the changing structure (increased nesting depth) on each > recursive call > > The initial call to t2 changes the structure of the > 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 parameter > > For the usual (no ) 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.
Re: [racket-users] Re: lambda and the equivalent define / "defun"
Just illustrates the changing structure (increased nesting depth) on each recursive call The initial call to t2 changes the structure of the 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 parameter For the usual (no ) function call It's not an issue for the standard car/cdr idiom of walking down recursive structures. On Wednesday, September 7, 2016 at 9:28:23 AM UTC-4, Jon Zeppieri wrote: > On Wed, Sep 7, 2016 at 8:33 AM, Sanjeev Sharmawrote: > Thanks for joining in. > > > > The amended question had nothing to do with the earlier example > > > > Okay. > > > > I'm wondering if there's a quick, standard (and easily understood) idiom > (without an internal helper function) to recur on the variable argument list y > > > > I'm still not sure what you mean, and I don't understand what your `t2` > example is supposed to do. > > > > -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.
Re: [racket-users] Re: lambda and the equivalent define / "defun"
On Wed, Sep 7, 2016 at 8:33 AM, Sanjeev Sharmawrote: > Thanks for joining in. > > The amended question had nothing to do with the earlier example > Okay. > > I'm wondering if there's a quick, standard (and easily understood) idiom > (without an internal helper function) to recur on the variable argument > list y > I'm still not sure what you mean, and I don't understand what your `t2` example is supposed to do. -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.
Re: [racket-users] Re: lambda and the equivalent define / "defun"
Thanks for joining in. The amended question had nothing to do with the earlier example I'm wondering if there's a quick, standard (and easily understood) idiom (without an internal helper function) to recur on the variable argument list y The flatten's not ideal - I may want to retain some sub-list structure (define x 4) (define t2 (lambda y (let ((z (car y))) (display y)(newline) (display (flatten y))(newline) (display z)(newline) (if (> x 0) (begin (set! x (- x 1)) ;(t2 (cdr y))) (t2 y)) (void) (t2 1 2 3 4 5 6) RETURNS (1 2 3 4 5 6) (1 2 3 4 5 6) 1 ((1 2 3 4 5 6)) (1 2 3 4 5 6) (1 2 3 4 5 6) (((1 2 3 4 5 6))) (1 2 3 4 5 6) ((1 2 3 4 5 6)) 1 2 3 4 5 6 (1 2 3 4 5 6) (((1 2 3 4 5 6))) (1 2 3 4 5 6) (1 2 3 4 5 6) 1 2 3 4 5 6 On Tuesday, September 6, 2016 at 9:04:07 PM UTC-4, Jon Zeppieri wrote: > On Tue, Sep 6, 2016 at 8:50 PM, Jon Zeppieriwrote: > > The `(lambda s ...)` is variable-arity, but when you call `ss` internally > [...] > > > Sorry, I realized after that your `ss` function essentially *is* a wrapper > around your `subsets` function -- which is exactly what I was suggesting. > However, there's no reason for it to duplicate so much of the work of > `subsets`. Since `subsets` works on any list, your definition of `ss` can > simply be: > > > (define (ss . xs) (subsets xs)) > > > The only purpose of `ss` is to package up its arguments as a list and pass > that list to `subsets`. > > > - 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.
Re: [racket-users] Re: lambda and the equivalent define / "defun"
On Tue, Sep 6, 2016 at 8:50 PM, Jon Zeppieriwrote: > > > > The `(lambda s ...)` is variable-arity, but when you call `ss` internally > [...] > Sorry, I realized after that your `ss` function essentially *is* a wrapper around your `subsets` function -- which is exactly what I was suggesting. However, there's no reason for it to duplicate so much of the work of `subsets`. Since `subsets` works on any list, your definition of `ss` can simply be: (define (ss . xs) (subsets xs)) The only purpose of `ss` is to package up its arguments as a list and pass that list to `subsets`. - 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.
Re: [racket-users] Re: lambda and the equivalent define / "defun"
On Tue, Sep 6, 2016 at 7:30 PM, Sanjeev Sharmawrote: > Thanks, that's helpful > > I am having trouble coming up with a quick and easy shorthand to recur on > the rest parameter - is there a standard way to cdr down this list without > an intermediary helper function that takes out the raise-nesting-depth > effect? > > Recursion on the base define (the one with the rest parameter) always adds > another layer of nesting for the rest parameter - the first time recurring > one must cdr, all the other times one must fiddle with car & cdr and what > I'm doing has been prone working for the first few iterations but > eventually arity-mismatches. > > >From your description, the arity mismatches are probably caused by the fact that you're defining `subsets` as a variable arity function, but internally the recursive uses assume that it takes a single list. Your code in the original post was: (define ss (lambda s (if (null? s) (list nil) (let ((rest (subsets (cdr s (append rest (map (lambda (x) (append (list (car s)) x)) rest)) The `(lambda s ...)` is variable-arity, but when you call `ss` internally, you're always passing a single list as the argument. If you want a variable-arity version, the simplest way is to define it as a simple wrapper around a recursive fixed-arity version. Alternatively, you could use `apply` in your recursive calls, but I'd call that bad style. -- 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.
[racket-users] Re: lambda and the equivalent define / "defun"
Thanks, that's helpful I am having trouble coming up with a quick and easy shorthand to recur on the rest parameter - is there a standard way to cdr down this list without an intermediary helper function that takes out the raise-nesting-depth effect? Recursion on the base define (the one with the rest parameter) always adds another layer of nesting for the rest parameter - the first time recurring one must cdr, all the other times one must fiddle with car & cdr and what I'm doing has been prone working for the first few iterations but eventually arity-mismatches. On Tuesday, September 6, 2016 at 7:39:59 AM UTC-4, gneuner2 wrote: > On Sun, 4 Sep 2016 10:36:21 -0700 (PDT), Sanjeev Sharma wrote: > > >two "x" 's also work > > > >(define list (lambda x x)) > > (lambda x x x) works because the evaluation of the middle x is a side > effect which is ignored. It still will work if you change it to, > e.g., (lambda x 'q x) or (lambda x 42 x) ... changing the middle x to > anything that is not a variable. > > > Extra information: [ignore if you know this already] > > (lambda x x) is the right way to define list ... but it works sort of > incidentally because Scheme permits "rest" parameters which gather > multiple arguments into a list. > > (define (list . x) x) also is equivalent and the syntax makes clear > that x is intended to be a rest parameter. > > > (lambda v v) is different from (lambda (v) v). The unadorned v in the > 1st indicates that v is a single rest parameter which will gather all > the arguments into a list. The parentheses in the 2nd indicate that v > is a normal parameter which will take on a single value [which may be > a deliberately passed list]. > > -> ((lambda v v) 1 2 3) > => '(1 2 3) > > -> ((lambda (v) v) 1 2 3) > => #: arity mismatch > > > Rest parameters may be combined with required parameters as in > (lambda (x . v) ... ) which is different from (lambda (x v) ... ). > The dot in the parameter 1st indicates that v is a rest parameter. In > the 2nd, both x and v are normal parameters. > > -> ((lambda (x . v) v) 1 2 3) > => '(2 3) > > -> ((lambda (x . v) x) 1 2 3) > => 1 > > -> ((lambda (x v) v) 1 2 3) > => #: arity mismatch > > -> ((lambda (x v) v) 1 2) > => 2 > > > A function can have only a single rest parameter [or none]. > > > Hope this helps, > George -- 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.
[racket-users] Re: lambda and the equivalent define / "defun"
On Sun, 4 Sep 2016 10:36:21 -0700 (PDT), Sanjeev Sharmawrote: >two "x" 's also work > >(define list (lambda x x)) (lambda x x x) works because the evaluation of the middle x is a side effect which is ignored. It still will work if you change it to, e.g., (lambda x 'q x) or (lambda x 42 x) ... changing the middle x to anything that is not a variable. Extra information: [ignore if you know this already] (lambda x x) is the right way to define list ... but it works sort of incidentally because Scheme permits "rest" parameters which gather multiple arguments into a list. (define (list . x) x) also is equivalent and the syntax makes clear that x is intended to be a rest parameter. (lambda v v) is different from (lambda (v) v). The unadorned v in the 1st indicates that v is a single rest parameter which will gather all the arguments into a list. The parentheses in the 2nd indicate that v is a normal parameter which will take on a single value [which may be a deliberately passed list]. -> ((lambda v v) 1 2 3) => '(1 2 3) -> ((lambda (v) v) 1 2 3) => #: arity mismatch Rest parameters may be combined with required parameters as in (lambda (x . v) ... ) which is different from (lambda (x v) ... ). The dot in the parameter 1st indicates that v is a rest parameter. In the 2nd, both x and v are normal parameters. -> ((lambda (x . v) v) 1 2 3) => '(2 3) -> ((lambda (x . v) x) 1 2 3) => 1 -> ((lambda (x v) v) 1 2 3) => #: arity mismatch -> ((lambda (x v) v) 1 2) => 2 A function can have only a single rest parameter [or none]. Hope this helps, George -- 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.
Re: [racket-users] Re: lambda and the equivalent define / "defun"
if anyone stumbles on this later this is also discussed in Kent Dybvig's TSPL4 http://www.scheme.com/tspl4/start.html#./start:h6 from section 2.6: For example, the definitions of cadr and list might be written as follows. (define (cadr x) (car (cdr x))) (define (list . x) x) -- 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.
Re: [racket-users] Re: lambda and the equivalent define / "defun"
merci & danker - I had just done same-parity exercise using this but did not see the equivalence -- 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.
Re: [racket-users] Re: lambda and the equivalent define / "defun"
I hope these examples are enough to help with your problem: ;-- #lang racket (define (negative . s) (map - s)) (negative 1 2 3 -4) ; ==> '(-1 -2 -3 4) (define (multiply-by n . s) (map (lambda (x) (* n x)) s)) (multiply-by 5 1 2 3) ; ==> '(5 10 15) (define (count . s) (length s)) (count 1 2 3 1 2 3) ; ==> 6 ;-- Gustavo On Sun, Sep 4, 2016 at 2:36 PM, Sanjeev Sharmawrote: > two "x" 's also work > > (define list (lambda x x)) > > -- > 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. -- 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.
[racket-users] Re: lambda and the equivalent define / "defun"
two "x" 's also work (define list (lambda x x)) -- 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.