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

From: Harry Spier (vasishtha.spier at gmail.com)
Date: Sat May 26 00:56:18 EDT 2012

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.


Posted on the users mailing list.