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
-------------------------------
For grins, I implemented a solution similar to the python solution
and uses generators more heavily, but I don't find it as clean
as the above:
------------------------------
import Utils
procedure main(args)
limit := integer(!args) | 5
t := "0"
every 1 to limit do {
every (s := "") ||:= !weave{!t, !map(t,"01","10")}
t := s
}
write(t)
end
-----------------------------
The "Utils" package is from the "Unilib" library at
http://tapestry.tucson.az.us/unicon, but that's only
to pick up the weave{} PDCO. The code for weave is just:
------------------------------
procedure weave(L)
suspend |@!L
end
------------------------------
It would be interesting to write a procedure that *generates* the
digits in the T-M sequence, instead of computing it via 'fixed-point'
values, but I have a feeling it would be both spectacularly difficult
and ugly...
--
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