[comp.lang.icon] return expressions

goer@SOPHIST.UCHICAGO.EDU (Richard Goerwitz) (07/10/89)

Why doesn't the following program output "go"?

procedure main()
  write(writeit())
end

procedure writeit()
  (return \stopit)
  return "go"
end

I figured that the variable stopit, being null, would
cause the return expression in which it is mentioned
to fail.  This would result only in expression failure,
however, and not in failure of the entire procedure.
I expected that control would then pass to the next
return expression, which would return the string "go"
- the string that would ultimately be written to the
stdout by the write function in the main procedure.

It looks, however, as though the expression

  return \stopit

is the same as 

  fail

at least in this instance, where \stopit fails.
Why is this so?  Why doesn't the program simply
print "go"?

                                        -Richard L. Goerwitz
                                        goer@sophist.uchicago.edu
                                        rutgers!oddjob!gide!sophist!goer

ralph@ARIZONA.EDU ("Ralph Griswold") (07/10/89)

The return expression always returns.  It returns the *outcome* of
evaluating its argument.  If its argument fails, the return causes
failure of the procedure invocation in which it occurs. E.g.,

	return &fail

has the same effect as

	fail

  Ralph Griswold / Dept of Computer Science / Univ of Arizona / Tucson, AZ 85721
  +1 602 621 6609   ralph@Arizona.EDU  uunet!arizona!ralph

sbw@naucse.UUCP (Steve Wampler) (07/11/89)

On Jul 10 at  1:54, Richard Goerwitz writes:
} 
} Why doesn't the following program output "go"?
} 
} procedure main()
}   write(writeit())
} end
} 
} procedure writeit()
}   (return \stopit)
}   return "go"
} end
} 
} It looks, however, as though the expression
} 
}   return \stopit
} 
} is the same as 
} 
}   fail
} 
} at least in this instance, where \stopit fails.
} Why is this so?  Why doesn't the program simply
} print "go"?
} 
One thing to keep in mind is that 'return' is a control regime
and not an operator.  While I can see arguments for the behavior
of 'return' going either way, I prefer it the way it is, as I
more often write things like:

	return f(x)

where I want the current function to fail if f(x) fails.

You might see if the control regime

	suspend \stopit

fits with what you're trying to do...

-- 
	Steve Wampler
	{....!arizona!naucse!sbw}

cheyenne@arizona.UUCP ("Cheyenne Wills") (07/11/89)

 
>You might see if the control regime
>
>        suspend \stopit
>
>fits with what you're trying to do...
 
Or you might want to try:
 
procedure writeit()
    return \stopit | "go"
end
 
Cheyenne Wills