unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Thien-Thi Nguyen <ttn@gnuvola.org>
To: guile-user@gnu.org
Subject: condition-case
Date: Fri, 20 Nov 2009 12:09:25 +0100	[thread overview]
Message-ID: <874oope9lm.fsf@ambire.localdomain> (raw)

[-- Attachment #1: Type: text/plain, Size: 96 bytes --]

Here is an implementation of the Emacs Lisp `condition-case',
along with a small test harness:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: z.scm --]
[-- Type: text/x-scheme, Size: 1357 bytes --]

(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))))

[-- Attachment #3: Type: text/plain, Size: 313 bytes --]


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

                 reply	other threads:[~2009-11-20 11:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874oope9lm.fsf@ambire.localdomain \
    --to=ttn@gnuvola.org \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).