On Wed, 2004-07-14 at 08:36, Steve Wampler wrote:
> On Wed, 2004-07-14 at 07:03, Edward A. Feustel wrote:
> > Doug McIlroy sent me the following. I am not familiar enough with generators to
> > provide
> > an authoritative answer. Can someone who can post the answer and send a copy to
> > Doug below?
> > Thanks.
> > Ed Feustel
> > -----------------------------------------------------------------------------------------
> >
> > I've been playing with using Python generators with
> > feedback--a far cry from the elegance of Haskell.
> >
> > I wondered how the stuff would look in Icon, but I
> > couldn't puzzle out the answer from the sketchy on-line
> > documentation. I'd have to read the book(!)
> >
> > You probably know the answer. How would you attack
> > the following problem in Icon?
> >
> ...
> >
> > Example. The Thue-Morse sequence is the fixed point of
> > the pair of equations
> >
> > (1) t = zipper(t,complement(t))
> > (2) t[0] = 0
> >
> > (zipper(.) interleaves elements from its two inputs
> > in alternation, and complement(.) exchanges 0 and 1.
> > The Thue-Morse sequence is 0110100110010110...)
> >
>
> I'm not sure this problem is really a good example of
> a problem that's suited to generators. Here's an
> Unicon (and Icon) program that computes Thue-Morse
> sequence values where generators don't play a
> significant role (just used to find the 'fixed point'
> the you want to display):
>
> --------------------------------
> procedure main(args)
>
> limit := integer(!args) | 5
>
> t := "0"
> every 1 to limit do {
> t := t || map(t,"01","10")
> }
> write(t)
>
> end
Here's the above code, rewritten as a procedure that generates
the 'fixed points' t[0],t[1],.... which is closer in spirit
to the python solution:
------------------------------
procedure main(args)
limit := integer(!args) | 5 # which fixed-point is shown
every t := thue_morse() \ (1+limit) # 0-based counting
write(t)
end
procedure thue_morse()
local t
suspend (t:=0) | |(t := t || map(t,"01","10"))
end
---------------------------
Note that thue_morse() produces an infinite sequence of the
fixed point values. I think the above maps into the
original problem statement fairly well.
--
Steve Wampler -- [EMAIL PROTECTED]
The gods that smiled on your birth are now laughing out loud.
-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
digital self defense, top technical experts, no vendor pitches,
unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
Unicon-group mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/unicon-group