Destructuring-bind and/or pattern matching
Posted: Sun Oct 07, 2012 9:46 pm
I know that Cobra supports multi assignment, but are there any plans to support those other features?
Discussion about the Cobra programming language.
http://cobra-language.com/forums/
int fib(int n) requires n >= 0 : "Fibonacci function is not defined for negative integers!";
fib(0) = 1;
fib(1) = 1;
fib(n) = fib(n-2) + fib(n-1);
enum State {Idle, On, Active}
class FinitiStateMachine {
transition( State s, String e ){ last = "Unknown"; }
transition( Idle, "TurnOn" ){ last = "TurnOn"; state = On; }
transition( On, "TurnOff" ){ last = "TurnOff"; state = Idle; }
transition( On, "Activate" ){ last = "Activate"; state = Active; }
transition( Active, "Walk" ){ last = "Walk"; }
transition( Active, "Run" ){ last = "Run"; }
transition( Active, "Talk" ){ last = "Talk"; }
transition( Active, "Stop" ){ last = "Stop"; state = On;}
}
static <A,B> List<C> mapPairs((A,B)->C f, List<A> xs, List<B> ys) {
List<C> result;
switch (Pair.Pair(xs, ys)) {
case Pair(List.Nil, _):
case Pair(_, List.Nil):
result = List.Nil;
break;
case Pair(List.Cons(A x, List<A> xs1),
List.Cons(B y, List<B> ys1)):
result = List.Cons(f(x, y), mapPairs(f, xs1, ys1));
break;
case _: System.out.println("case 2"); break;
}
return result;
}
sig MapFunction(a as int);
def map( [] ) = 0
def map( [a : rest ] as List<int>, c as MapFunction):
return [ c(a) : map(rest, c) ]