From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!j12g2000vbl.googlegroups.com!not-for-mail From: Gene Newsgroups: comp.lang.ada Subject: Continuation Passing Style in Ada Date: Thu, 7 May 2009 20:36:20 -0700 (PDT) Organization: http://groups.google.com Message-ID: NNTP-Posting-Host: 134.240.200.3 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1241753780 23549 127.0.0.1 (8 May 2009 03:36:20 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 8 May 2009 03:36:20 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: j12g2000vbl.googlegroups.com; posting-host=134.240.200.3; posting-account=-BkjswoAAACC3NU8b6V8c50JQ2JBOs04 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; MS-RTC LM 8; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729),gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:5736 Date: 2009-05-07T20:36:20-07:00 List-Id: 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;