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

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun May 27 14:10:47 EDT 2012

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 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 (in-list l)])
>    (unless? (or (prior?) (next?))
>                 (displayln (/ (+ x (prior x) (next x)) 3))))))
> 
> From a functional programming perspective is building a form like this
> a bad idea?
> 
> Thanks,
> Harry
> 
> 
> On Sun, May 27, 2012 at 4:32 AM, Jens Axel Søgaard
> <jensaxel at soegaard.net> wrote:
>> 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.
>> In the fold version it is only traversed once.
>> 
>> /Jens Axel
>> 
>> 
>> 2012/5/27 Harry Spier <vasishtha.spier at gmail.com>:
>>> Thank you Matthias for the pattern, its exactly what I needed.
>>> And thank you Jens for the trick of appending a dummy element to the
>>> list so you don't lose the last item in the list.
>>> I.e.
>>> (define (f1 lst)
>>>  (define-values (result-list dummy1 dummy2)
>>> (for/fold ([result-list '()][prior '()] [current (first lst)])
>>>  [next (rest (append lst '()))]
>>>  ....
>>>  ...
>>>  (values (cons result-item result-list )
>>>          current
>>>          next))) (reverse result-list))
>>> 
>>> On Sat, May 26, 2012 at 11:50 AM, Matthias Felleisen
>>> <matthias at ccs.neu.edu> wrote:
>>>> 
>>>> Do you mean that if you operated on a list it would look like this:
>>>> 
>>>> #lang racket
>>>> 
>>>> (define (running-average-of-3 l2)
>>>>  (define-values (_1 _2)
>>>>    (for/fold ((current (second l2)) (prior (first l2))) ((next (rest (rest l2))))
>>>>      (displayln (/ (+ prior current next)))
>>>>      (values next current)))
>>>>  (void))
>>>> 
>>>> (running-average-of-3 '(1 2 3 4 5 6 7 8 9 0))
>>>> 
>>>> 
>>>> 
>>>> 
>>>> On May 26, 2012, at 12:56 AM, Harry Spier wrote:
>>>> 
>>>>> I can use for/fold to loop through a sequence while having the current
>>>>> element and  the previous element of the sequence still available by
>>>>> doing something like this.
>>>>> 
>>>>> (for/fold ([previous-element-in-sequence '()][list-being-created '()])
>>>>>  ( [current-element-in-sequence sequence])
>>>>>  (do some stuff)
>>>>>  ....
>>>>>  (values current-element-in-sequence
>>>>>          (cons something list-being-created)))
>>>>> 
>>>>> But what I want is to be able to loop through a sequence while having
>>>>> the prior, the current and the next element available.
>>>>> 
>>>>> I'm sequencing through a representation of lines of a black and white
>>>>> page of text showing where the black pixels are and from that
>>>>> producing a graph showing the connectivity of the black pixel strips
>>>>> on each line to the ones on the lines above and below it.
>>>>> 
>>>>> Using for/fold I'm able to code the graph of connectedness of the
>>>>> black strips on each line to the prior line in a very straightforward
>>>>> way, but I'd like to graph in both directions both to the prior line
>>>>> and to the next line at the same time.
>>>>> 
>>>>> Is there a simple way to loop through a sequence and have the prior,
>>>>> current and next element available at the same time.
>>>>> 
>>>>> Thanks,
>>>>> Harry
>>>>> ____________________
>>>>>  Racket Users list:
>>>>>  http://lists.racket-lang.org/users
>>>> 
>> 
>> 
>> 
>> --
>> --
>> Jens Axel Søgaard
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



Posted on the users mailing list.