Mercurial > repos > rhope
view fib.rhope @ 57:2174878a6e4b
Avoid unnecessary nested switch
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Thu, 08 Oct 2009 02:12:25 -0400 |
parents | 76568becd6d6 |
children | a163250b8885 |
line wrap: on
line source
/* This example program contains a naive implementation of the Fibonacci function While this isn't a particular fast way to implement the Fibonacci function it does parallize nicely */ //Here we define a worker Fib with one input named 'n' and one output named 'out' Fib[n:out] { //The If worker is one way to conditionally execute a piece of code If[[n] < [2]] { //This line will execute if [n] < [2] evaluates to Yes out <- 1 }{ //This line will execute if [n] < [2] evaluates to No //All Worker calls can be expressed in infix, postfix, or prefix notation //So [n]-[1] is the same as -[n,1] and [n,1]- out <- [Fib[[n]-[1]]] + [Fib[[n]-[2]]] } } Main[args] { //Here we get the first command line argument and convert it to a number //Yes I realize this is incredibly ugly looking n <- <String@Whole Number[[args]Index[1]] //Call our Fib worker and Print the result to the terminal Print[Fib[n]] }