From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Thien-Thi Nguyen Newsgroups: gmane.lisp.guile.user Subject: condition-case Date: Fri, 20 Nov 2009 12:09:25 +0100 Message-ID: <874oope9lm.fsf@ambire.localdomain> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1258715492 10108 80.91.229.12 (20 Nov 2009 11:11:32 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 20 Nov 2009 11:11:32 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Fri Nov 20 12:11:25 2009 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1NBROe-0006dv-RW for guile-user@m.gmane.org; Fri, 20 Nov 2009 12:11:25 +0100 Original-Received: from localhost ([127.0.0.1]:35594 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NBROe-0005AF-28 for guile-user@m.gmane.org; Fri, 20 Nov 2009 06:11:24 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NBROY-00056u-Ie for guile-user@gnu.org; Fri, 20 Nov 2009 06:11:18 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NBROV-00051k-T7 for guile-user@gnu.org; Fri, 20 Nov 2009 06:11:17 -0500 Original-Received: from [199.232.76.173] (port=35595 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NBROV-00051N-LU for guile-user@gnu.org; Fri, 20 Nov 2009 06:11:15 -0500 Original-Received: from smtp-out113.alice.it ([85.37.17.113]:4844) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NBROV-0004Rc-7X for guile-user@gnu.org; Fri, 20 Nov 2009 06:11:15 -0500 Original-Received: from FBCMMO01.fbc.local ([192.168.68.195]) by smtp-out113.alice.it with Microsoft SMTPSVC(6.0.3790.3959); Fri, 20 Nov 2009 12:11:11 +0100 Original-Received: from FBCMCL01B05.fbc.local ([192.168.69.86]) by FBCMMO01.fbc.local with Microsoft SMTPSVC(6.0.3790.3959); Fri, 20 Nov 2009 12:11:09 +0100 Original-Received: from ambire.localdomain ([95.237.68.234]) by FBCMCL01B05.fbc.local with Microsoft SMTPSVC(6.0.3790.3959); Fri, 20 Nov 2009 12:11:08 +0100 Original-Received: from ttn by ambire.localdomain with local (Exim 4.63) (envelope-from ) id 1NBRMj-0002TN-JJ for guile-user@gnu.org; Fri, 20 Nov 2009 12:09:25 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux) X-OriginalArrivalTime: 20 Nov 2009 11:11:08.0937 (UTC) FILETIME=[293CA790:01CA69D2] X-detected-operating-system: by monty-python.gnu.org: Windows 2000 SP4, XP SP1+ X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:7502 Archived-At: --=-=-= Here is an implementation of the Emacs Lisp `condition-case', along with a small test harness: --=-=-= Content-Type: text/x-scheme Content-Disposition: inline; filename=z.scm (define-macro (condition-case var bodyform . handlers) `(catch #t (lambda () ,bodyform) (lambda ,var (case (car ,var) ,@(map (lambda (h) ;; Emacs Lisp `case' handles lone symbols, ;; which is pleasant. `(,(let ((name (car h))) (if (or (eq? 'else name) (pair? name)) name (list name))) ,@(cdr h))) handlers) ,@(if (assq 'else handlers) '() `((else ;; This copy is to workaround a (possibly misguided) ;; `nconc2last' used in the Guile 1.4.x implementation. (let ((copy (list-copy ,var))) (apply throw (car copy) (cdr copy)))))))))) ;;; (put 'condition-case 'scheme-indent-function 2) (define (read-string s) (call-with-input-string s read)) (write-line (condition-case c (if (pair? (cdr (command-line))) (apply throw (map read-string (cdr (command-line)))) 'ok) (bad (simple-format #t "BAD: ~S !~%" (cdr c)) 42) ((a b c) (write c) (newline) (cdr c)) (else (let ((key (car c)) (args (cdr c))) (write-line "ELSE!") (write key) (newline) (write args) (newline) 'else)))) --=-=-= To play, save to (say) /tmp/z.scm, and invoke from command line, e.g.: guile -s /tmp/z.scm guile -s /tmp/z.scm bad stuff happens guile -s /tmp/z.scm no bad stuff happens I wonder if there is a better way, where better means "more idiomatic" or "standardardized", or "more elegant", or what have you. thi --=-=-=--