Re: Why doesn't foreach return a value



The post title is misleading. In the typical Tcl:

% set a [foreach b {e f g} {}]
% puts $a

%

You see that a has a value, so the code [...] does return a value. But
the larger question is not why it returns or doesn't return a value,
but 'when' should it return it, and what value should it return?
Foreach actually never 'goes away', so how could it return? So it
really boils down to how it is called. Is it called as a function, or
is is simply used in the current flow of code? If it is called as a
function, it does return 'a value'. But, regardless of this, there is
simply no other value it should 'return'. If there is, then you need
to first identify what that value should be. I doubt you will find any
logical support for anything in particular.

The most obviously useful information would be some kind of metadata
which documents what happened, mostly for the case of continuing, and
maybe what state the loop was in upon exit. However, these metadata re
not really part of a normal program (if they were, you could just add
variables to record the information). They may aid debugging.

There is an instrumented for/foreach/while in the new AOLserver code:

http://aolserver.cvs.sourceforge.net/aolserver/aolserver/nsd/tclloop.c?view=markup

This allows you to find all running loops and perform: "list", "info",
"pause", "resume", "cancel", "eval", "install". I assume this code was
added to track down misbehaving loops and figure out why they are not
working without needing to restart an entire process (plus you can't
easily debug a loop error without live data).

.



Relevant Pages

  • Re: deaf grandma.
    ... puts "Simon says, ... and check the flag each time to see whether we loop again. ... puts "that was in all caps" ...
    (comp.lang.ruby)
  • [SUMMARY] Word Loop (#149)
    ... cheater is a wonderful label for a programmer to have. ... the first priority is to find a possible loop. ... puts before+letter+looplets.shift ...
    (comp.lang.ruby)
  • Re: TCL Need help
    ... I have a simple script like this. ... puts i=$i;set i 11} ... enters into indefinite while loop which according to one of the book ... being evaluated at the beginning leading to an infinite loop. ...
    (comp.lang.tcl)
  • Re: Help With Homework Code
    ... puts "Would you like to Roll or Pass?" ... The empty parentheses are typically omitted. ... you could also simply call 'break' here and use the 'loop' ...
    (comp.lang.ruby)
  • Re: Threads - running something in background
    ... I've been studying Ruby for some weeks now but when it comes to ... puts upd.i ...
    (comp.lang.ruby)