//MC# Examples
// nth Fibonacci number computation
//  Usage:   Fib.exe  < n >

using System;
public class Fib
{
 public handler Get2 long() & channel c1( long x ) & channel c2( long y ) {
  return ( x + y );
 }

 public async Compute( long n, channel( long ) c )
 {
  Console.WriteLine( "Compute: n=" + n );

  if ( n <= 1 )
   c ! ( cfib( n ) );
  else
  {
   new Fib().Compute( n-1, c1 );
   new Fib().Compute( n-2, c2 );
   c ! ( (long)Get2 ? () );
  }
 }

 private long cfib( long n )
 {
  if ( n <= 1 )
   return ( n );
  else
   return ( cfib( n - 1 ) + cfib( n - 2 ) );
 }
}

public class ComputeFib
{

 public handler Get long() & channel c( long x ) {
  return ( x );
 }

 public static void Main( string[] args )
 {
  if ( args.Length < 1 )
  {
   Console.WriteLine( "Usage: Fib.exe number" );
   return;
  }
  int n = System.Convert.ToInt32( args [ 0 ] );
  ComputeFib cf = new ComputeFib();
  Fib fib = new Fib();

  fib.Compute( n, cf.c );

  Console.WriteLine( "For n = " + n + " value is " + cf.Get?() );
 }
}