3.4. Otherwise Combinator

F ; G

The execution of F ; G proceeds as follows. First, F is executed. If F halts, and has not published any value, then G executes. If F publishes one or more values, then G is ignored. The publications of F ; G are those of F if F publishes, or those of G if F is silent.

3.4.1. Syntax

[14]Otherwise::= Expression ; Expression  

Combinator Precedence Level: sequential > parallel > pruning > otherwise [Full Table]

3.4.2. Notable Identities

F ; G ; H = (F ; G) ; H (Left Associative)
F ; G ; H = F ; (G ; H) (Right Associative)

3.4.3. Type

The type of F ; G is the join of the types of F and G.

3.4.4. Examples

Fall-back Search
include "search.inc"

{- Attempt to retrieve search results from Google.
   If Google does not respond, then use Yahoo.
-}  

Google("cupcake") ; Yahoo("cupcake")
Lexicographic sublist
{- A call to sum(n, xs), where n is an integer and xs is a list
   of integers, find the first sublist of xs lexicographically
   whose elements add up to n.  The call publishes nothing if
   there is no solution
-}

def sum(0,[]) = []
def sum(n,[]) = stop
def sum(n, x:xs) =
  x:sum(n-x, xs) ; sum(n, xs)
  
sum(-5,[-2,5,1,4,8,-7])

{-
OUTPUT:
[-2, 4, -7]
-}
Channel Transfer
{- Transfer all items from a channel to a list.  Assume that the
   process has exclusive access to the channel, so that no other
   process is adding or removing items.
-}

def xfer(ch) =
  ch.getD() >x> x:xfer(ch) ; []

val ch = Channel()

ch.put(1) >> ch.put(2) >> ch.put(3) >> ch.put(4) >> xfer(ch) 

{-
OUTPUT:
[1, 2, 3, 4]
-}
Helpful publication
{- Publish a list of all publications of f.
   Assume f is helpful.  Assume you have xfer() from above.
-}

f() >x> b.put(x) >> stop ; xfer(b)

{-
NONRUNNABLE
-}

{- (1 | 2 | 3) >x> c.put(x) >> stop ; xfer(c) outputs
   [1,2,3]
-}

3.4.5. Related Links

Related Tutorial Sections