Re: [racket] Help with filter-map

2012-05-27 Thread Neil Van Dyke
One of the problems with this is that "(stream->list (in-range 2 100))" is constructing a list of approx. 1 million elements just to count a number by 1. Counting a number by 1 can instead be done by: (+ 1 number) The fancy iterators like "for" can do secret tricks to make things like "(

[racket] Help with filter-map

2012-05-27 Thread Joe Gilray
I created some Racket solutions to ProjectEuler #71. Below is one that uses filter and map: (define (euler71c) (numerator (apply max (filter (λ (n) (> (/ 3 7) n)) (map (λ (n) (/ (floor (/ (* n 3) 7)) n)) (stream->list (in-range 2 100))) 1) I thought I might speed it up by using filter-

Re: [racket] Looping with look-behind and look-forward

2012-05-27 Thread Sam Phillips
On Sun, May 27, 2012 at 8:46 AM, Harry Spier wrote: > (define (f1 l) >   (for/fold ([result-list '()]) >    ( [prior (in-list (append '() l))] >      [x (in-list l)] >      [next (in-list  (append (rest l) '()))]) >    ... >    ... make new-result-element using x prior and next... >   (cons new-re

Re: [racket] Looping with look-behind and look-forward

2012-05-27 Thread Neil Van Dyke
I do see a plausible potential feature addition to the standard sequence iterators like "for/fold". (I don't need it myself, since I don't use the iterators, but I can see that other people might.) I have occasionally implemented and used a list-processing "map" or "fold" that provides the pr

Re: [racket] Looping with look-behind and look-forward

2012-05-27 Thread Matthias Felleisen
While I see the apparent declarative nature of the (next .) and (prior .) notation, I am afraid that you would next expect (next (next (next (next (next x)) to work too. And that would be bad. -- Matthias On May 27, 2012, at 9:30 AM, Harry Spier wrote: > Is the "for" form a macro? And i

Re: [racket] Looping with look-behind and look-forward

2012-05-27 Thread Jens Axel Søgaard
You could consider using vectors. #lang racket (define (running-average-of-3-vector-edition xs) (define n (vector-length xs)) (define (prev i) (vector-ref xs (- i 1))) (define (next i) (vector-ref xs (+ i 1))) (for ([(x i) (in-indexed xs)] #:when (< 0 i (- n 1))) (displayln (/

Re: [racket] Looping with look-behind and look-forward

2012-05-27 Thread Harry Spier
This is just a simple example to work on the form. In my application I'm not processing integers, I'm processing a list of sub-lists. Each sublist is of the form (number (s . e ) ...) On Sun, May 27, 2012 at 12:16 PM, Richard Cleis wrote: > If you are only using integers, efficiency matters, an

Re: [racket] Looping with look-behind and look-forward

2012-05-27 Thread Richard Cleis
If you are only using integers, efficiency matters, and your running averages are larger than two (I can't tell of you are using three for a simple example, or if that is the only requirement), then it might be better to maintain the latest sum by subtracting the oldest member and adding the new

Re: [racket] Looping with look-behind and look-forward

2012-05-27 Thread Harry Spier
Sorry Jens, I see I've just recreated the form you originally suggested. Thanks, Harry On Sun, May 27, 2012 at 11:46 AM, Harry Spier wrote: > I think I've come up with a simpler form to do this. > > (define (running-average-of-3 l) >   (for >    ( [prior (in-list l)] >      [x (in-list (rest l))]

Re: [racket] Looping with look-behind and look-forward

2012-05-27 Thread Harry Spier
I think I've come up with a simpler form to do this. (define (running-average-of-3 l) (for ( [prior (in-list l)] [x (in-list (rest l))] [next (in-list (rest (rest l)))]) (displayln (/ (+ x prior next) 3 (running-average-of-3 '(1 2 3 4 5 6)) So I can do something like:

Re: [racket] Looping with look-behind and look-forward

2012-05-27 Thread Jens Axel Søgaard
2012/5/27 Harry Spier : > Is the "for" form a macro?  And if so how difficult would it be to > make a new form "for-with-look-around" that had built in look-back and > look-ahead operators (next x) (prior x) (  next?) (prior?). > > So you could do something like: > > ((define (running-average-of-3

Re: [racket] Looping with look-behind and look-forward

2012-05-27 Thread Harry Spier
Is the "for" form a macro? And if so how difficult would it be to make a new form "for-with-look-around" that had built in look-back and look-ahead operators (next x) (prior x) ( next?) (prior?). So you could do something like: ((define (running-average-of-3 l) (for-with-look-around ([x

Re: [racket] Looping with look-behind and look-forward

2012-05-27 Thread Jens Axel Søgaard
It suddenly dawned on me, that the append trick is not needed. This works too: (define (running-average-of-3-alternative l) (for ([x (in-list l)] [y (in-list (cdr l))] [z (in-list (cddr l))]) (displayln (/ (+ x y z) 3 But in this version, the list is traversed 3 times. I