I love the code that comes across this list.
Since I rarely get to write Icon at work, it's nice to get reminders of
how to use it well.  I further appreciate reminders of how to use
threads, given that I have trained myself to avoid threaded algorithms
by using co-expressions.

For what it's worth:  When I do get to write Icon at work, I'm usually
trying to get a solution to a problem in the absolute minimum time
possible and I estimate that Icon will be the shortest path; thus, I
turn to the Icon Program Library if I can.  This problem brings to mind
Ralph Griswold's stree routine from trees
  http://www.cs.arizona.edu/icon/library/src/procs/trees.icn
which converts a tree whose nodes are convertible to strings to a string.
   procedure stree(ltree)  #: construct string from tree
      local s

      if *ltree = 1 then return ltree[1]
      s := ltree[1] || "("
      every s ||:= stree(ltree[2 to *ltree]) || ","
      return s[1:-1] || ")"

  end

Since it uses parentheses as the grouping symbol, I might grab
and modify the routine, or I might just use sed or vi to do a global
search and replace on the output.  Without making this change, my code
is this:
   link trees
   procedure writeany(clump)
      write(stree(clump))
   end
   procedure main()
      writeany(["a", [2, ["c", 4]], ["e", 6]])
   end

Best regards,
- Art


On 3/10/2014 11:42 PM, Jafar Al-Gharaibeh wrote:
> Here is my solution extended to handle list of arbitrary depths, using
> threads this time instead of co-expressions. I actually had to commit a
> tweak to Unicon to get threads working in the place of co-expressions in
> this case. The advantage is that a thread would have "some" results
> evaluated and ready to be consumed by the caller before the caller even
> asks for them; a form of simple "implicit" concurrency.
> 
> procedure wl(l)  # writes
>     C := thread !l
>     writes("[")
>     wl(type(c:=@C)=="list" & c) | writes(c)
>     while c:=@C & writes(", ") do (wl(type(c)=="list" & c) | writes(c))
>     writes("]")
>     return
> end
> 
> procedure writeany(l)  #write
>     wl(l)
>     write("")
> end
> 
> 
> I tested using
>     l := ["a", ["b", ["c", "d"]], ["e", "f"]]
> in your original program and I got this output:
> 
> [a, [b, [c, d]], [e, f]]
> [[b, [c, d]], [e, f]]
> [[e, f]]
> []
> [d, o, g, s]
> 
> 
> Cheers,
> Jafar
> 
> 
> 

------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
Unicon-group mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to