From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: gnuist006@gmail.com Newsgroups: gmane.emacs.help Subject: Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter Date: Tue, 23 Oct 2007 18:06:58 -0000 Organization: http://groups.google.com Message-ID: <1193162818.247085.305430@q3g2000prf.googlegroups.com> References: <1192913158.922454.108100@k35g2000prh.googlegroups.com> <13hl1rug786p1f2@corp.supernews.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: ger.gmane.org 1193164845 1767 80.91.229.12 (23 Oct 2007 18:40:45 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 23 Oct 2007 18:40:45 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Oct 23 20:40:45 2007 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1IkOgE-0006Ca-G5 for geh-help-gnu-emacs@m.gmane.org; Tue, 23 Oct 2007 20:40:42 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IkOg6-0007Cd-Lq for geh-help-gnu-emacs@m.gmane.org; Tue, 23 Oct 2007 14:40:34 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!q3g2000prf.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.lang.c,comp.lang.c++ Original-Lines: 86 Original-NNTP-Posting-Host: 75.28.129.58 Original-X-Trace: posting.google.com 1193162818 8014 127.0.0.1 (23 Oct 2007 18:06:58 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Tue, 23 Oct 2007 18:06:58 +0000 (UTC) In-Reply-To: <13hl1rug786p1f2@corp.supernews.com> User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.1.8) Gecko/20071008 Firefox/2.0.0.8,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: q3g2000prf.googlegroups.com; posting-host=75.28.129.58; posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0 Original-Xref: shelby.stanford.edu gnu.emacs.help:153240 comp.lang.c:828359 comp.lang.c++:954491 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:48727 Archived-At: On Oct 20, 3:55 pm, "Alf P. Steinbach" wrote: > * gnuist...@gmail.com: > > > > > I have some code like this: > > > (if (test) > > (exit) > > (do something)) > > > or > > > (if (test) > > ( do something) > > (exit)) > > > Various levels of nestings. > > > I have several questions, basic to sophisticated. > > > (1) What is the lisp equivalent idiom for (exit) as in bash or > > in C. > > C++ does not have a built-in 'exit' command. There is a library > function 'exit' which exits the process. One must assume that's not > what you mean, and that you're not asking C and C++ programmers to teach > you Lisp. > > Therefore, assuming you want to exit the function or the block. > > > (2) What is the best practice to handle this kind of problems? > > It's not a general class of problem. > > Appropriate solutions depend on the problem at hand. > > E.g., in C++, > > // (if (test) (exit) (do something)) > > void foo() > { > if( !test ) > { > doSomething(); > } > } > > void bar() > { > if( test ) { return; } > doSomething(); > } > > > (3) What is the intermediate practice to handle this kind of > > problems. > > ? > > > NOTE: I am really afraid of try-catch-throw. I have never been > > able to understand it since it does not exist in C and I cant > > really visualize the construct in terms of C. That is what my > > brain can process. If you understand it so well, you can show > > me how one would really implement that kind of construct in > > C and then by extension I can see that kind of program flow > > in LISP. Whether its imperative programming or functional, > > beneath there is program counter and assembly. C is close > > to machine so much that it is almost assembly. So understanding try-c- > > t in C is equivalent to understanding at > > the level of machine language. > > The closest equivalent in C would be a 'longjmp'. However, a C++ > exception is more limited, in that it will only jump up the call chain, > and it's more powerful, in that it will destroy local objects as it does > so. Also, if you use 'longjmp' in C++ you're practically doomed (unless > you use it to jump between co-routines with their own stacks), because > 'longjmp' doesn't destroy local objects. Sure you have good ideas. I still would like an equivalent implementation explained. Ofcourse, smart companies and smart programmers were doing all this before C++ came and even in LISP they have atleast two of try catch throw.