On Tue, 2003-10-28 at 00:21, Hiroshi Shinohara wrote:
> I have a problem with generator.
...

> ####################
> # write output of generator
> # 1,2,..,10 -> "1 2 3 .. 10" ?
> # gentest2.icn
> 
> procedure main()
>   write(&version)
>   gen_out(seq() \10)
> end
> 
> procedure gen_out(gen,s)
>   /s := " "
>   n := 0
>   every ss := gen do {
       # The variable gen is not the above seq()\10 expression,
       #   but rather holds one value from the above expression.
       #   So, the every here does nothing - there's nothing
       #   that produces more than one result.
>     if n > 0 then writes(s,ss)
>              else writes(ss)
>     n +:= 1
>   }
>   write()
> end
> 
> But gentest2 writes
> Icon Version 9.3.2.  July 1, 1999
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> 
> I cannot understand the behaviour of "gen_out".
> Why "gen_out" does not work as I expect ?
> How I change "gen_out" to write generator's output in one line ?

A generator only produces results as needed, so you'd be better
off in main writing:

    every gen_out(seq()\10)

(it actually would work as written since gen_out() ultimately
fails, but it's better to make it clear that's what you want.)

What you want (I think) is actually pretty tricky to do!
(At least when first learning Unicon.)

The problem is that gen_out() itself knows nothing about it's
calling environment and so doesn't know that it's got a generator
producing values for its first parameter - so there's no way for
gen_out() to 'know' when the last value has been produced by
the external generator.  Similarly, it has no easy way to tell
that it's got the first call (especially if your definition of
'first' is 'first in the current evaluation').  So, it's probably
best to put the prefix and suffix outputs outside of gen_out():

   procedure main()
       writes(" ")
       every seq_out(seq()\10)
       write()
   end

   procedure seq_out(val)
       writes(val)
   end

but, of course, seq_out() really isn't needed now and you're back
to (pretty much) your original solution:

    procedure main()
        writes(" ")
        every writes(seq()\10)
        write()
    end

If you really want a procedure that takes as argument an arbitrary
result sequence and writes it out, you'll have read up on
co-expressions (and, more specifically, PDCOs [programmer-defined
control operations]).
       
I hope this helps!     
-- 
Steve Wampler <[EMAIL PROTECTED]>


-------------------------------------------------------
This SF.net email is sponsored by: The SF.net Donation Program.
Do you like what SourceForge.net is doing for the Open
Source Community?  Make a contribution, and help us add new
features and functionality. Click here: http://sourceforge.net/donate/
_______________________________________________
Unicon-group mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to