From: Gene <gene.ressler@gmail.com>
Subject: Continuation Passing Style in Ada
Date: Thu, 7 May 2009 20:36:20 -0700 (PDT)
Date: 2009-05-07T20:36:20-07:00 [thread overview]
Message-ID: <f405484c-c5cf-4f95-a549-b90b8c2232f2@j12g2000vbl.googlegroups.com> (raw)
As a recreation, I've been coding, in different languages, a little
program that prints all possible ways to parenthesize a given
arithmetic expression. A way to avoid storing all the possiblities is
to use continuation passing style. The Ada version is quite pleasing,
so thought I'd share it. (You don't want to see the C version):
with Ada.Text_IO;
procedure Paren_Search is
procedure Parenthesize(Expr : in String;
Continue : access procedure (Expr : in
String)) is
Op : Positive := Expr'First + 1;
procedure Parenthesize_Rhs(Lhs : in String) is
procedure Apply_Op(Rhs : in String) is
begin
Continue("(" & Lhs & Expr(Op) & Rhs & ")");
end Apply_Op;
begin
Parenthesize(Expr(Op + 1 .. Expr'Last), Apply_Op'Access);
end Parenthesize_Rhs;
begin
if Expr'Length = 1 then
Continue(Expr);
else
while Op < Expr'Last loop
Parenthesize(Expr(Expr'First .. Op - 1),
Parenthesize_Rhs'Access);
Op := Op + 2;
end loop;
end if;
end Parenthesize;
begin
Parenthesize("2/2-3/3-4/4-5/5", Ada.Text_IO.Put_Line'Access);
end Paren_Search;
reply other threads:[~2009-05-08 3:36 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox