For a change this is an exercism problem I know something about.
I did look at the exercism web site, and did all the SML exercises,
but could not find any for Smalltalk or Pharo.
The exercise is in fact about using a stack.
stack <- empty
for each character of the string
if it is one of ( [ { push it on the stack
if it is one of ) ] }
if the stack is empty or the top element does not
correspond to this character, return false
if it is not one of ( ) [ ] { } just ignore it
return (the stack is empty)
BOTH of the "return" constructs in this pseudo-code become
^ something
in Smalltalk. "Long returns", where "^" occurs inside a block,
are well defined, very useful, and universally accepted in Smalltalk.
There is no analogue of "^" for returning from a block.
By the way, this has nothing to do with #do:. It's about
returning from the method through any number of blocks.
For example, you will sometimes see code like
x := aDictionary at: aKey ifAbsent: [^false].
On Fri, 5 Apr 2019 at 00:16, Roelof Wobben <[email protected]> wrote:
> Hello,
>
> For a challenge of Exercism I need to check if some parenthes and
> brackets are balanced.
>
> so for example
>
> () = true
> ([]) = true
>
> but (]) is not true because the bracket has no opening bracket.
>
> Now I wonder if I can premature end a #do: like this
>
> collection do: [:element | (condition with element) ifFalse: [^false]]
>
> in pseudo code
>
> is this a valid way to end the do ?
> or is there a better way ?
>
> Roelof
>
>