From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "Dr. Arne Babenhauserheide" Newsgroups: gmane.lisp.guile.user Subject: Syntax-Case macro that selects the N-th element from a list Date: Mon, 05 Apr 2021 13:30:21 +0200 Message-ID: <87im5155pu.fsf@web.de> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10612"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.4.15; emacs 27.2 To: Guile User Mailing List Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Mon Apr 05 13:30:53 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lTNRU-0002fi-M5 for guile-user@m.gmane-mx.org; Mon, 05 Apr 2021 13:30:52 +0200 Original-Received: from localhost ([::1]:46702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lTNRT-0001nA-Ke for guile-user@m.gmane-mx.org; Mon, 05 Apr 2021 07:30:51 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:45332) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lTNRF-0001mw-JP for guile-user@gnu.org; Mon, 05 Apr 2021 07:30:37 -0400 Original-Received: from mout.web.de ([212.227.15.14]:43057) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lTNRC-0003tA-5e for guile-user@gnu.org; Mon, 05 Apr 2021 07:30:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1617622228; bh=2FZs3A1QYxXpeiBYmNqHQL+DmOHtIVjGQ3wJD6pfSJw=; h=X-UI-Sender-Class:From:To:Subject:Date; b=DtdcA0QujkV1K3Xi7IiSpYDQvOKZRoAVA+Nza3fYZMK2KirI84PvbwFCBFX6i+KYt IhjOJYOm7H+yJRzFjS2Q7DcdOzibGxMHGrQaaKQa5iPypceqmf3WGFfxWKY27eYfc7 4F84VQPnJ/f6jOy+wo67sC/0R78BTuJWxpu/6Lvk= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Original-Received: from fluss ([84.149.81.26]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1M5j5y-1lV1KQ17fO-007HWi; Mon, 05 Apr 2021 13:30:28 +0200 X-Provags-ID: V03:K1:p20mx77yPokT9b62iR4x4+LQBLe6Naz8Z8OmREEeNAvT4tzyPi3 3QAx4HscpJZRDjwyTHBx9vAJCXlqN+rB0ZklRx/PHje8W+UPpI7qUQ4JZIqvKmL+DscuXae B++flZdFHJfskuBofrabd6MTcU5nXAFKIIlRUxYXHhSW4i/vSmvGF5xyXwwtWY/2rCNw5BQ pychB2/1XyaZdNTTzJKpQ== X-UI-Out-Filterresults: notjunk:1;V03:K0:jwWjFw6Wh2o=:LTE9fd6DawihMwXnmfxllf xlYruU9j2at44BqNTeM/dpqtZLuNWtuID1MFfy/VikXtafgK6Dsa3r+IWhbBD/0pco3cT8WSx lgK4T7Is2S3Gog3w5bRDJd5yc6LNGLBdyeFA8eKSF0R5JCsQtuj/ZnyEB3qOZYXCXxop0Jnq+ cWZmAgVZ4P/3k0gipM36iTYKH57K5ZPcJvMs1nQiD0YGlSjxa1UCL/926HrJLJd1hJR+FKOXr LaWU+OFIpJfbKHWjiUIY3fqczRnmWMi92rDTb0DP0nW0doi3XvJ7MWBiz9Dmj8Mv69Bbfkhcq cGEO3hmnTpXhzQF88fXP8EKhGd8xeZ8y2RrJKVb03d5xWCeIjPPl8glkQ99YqhvWW6kIPym2a G8Egssu+63ZEkOe1nrnFoeTvvZhVN1f4gz8O22kEB+80fAkCoF7KqDE3N5qaMYayU5JS1ek/N x8oZJzxQTK/P+13lB31o+1Otfp6ATU09ZWmkV5qkXwR/FMcofyAKATFZjP/Q7LYNfo8N409bu JEElGBjM4H6bQ+9kD9oRWgi8zJyEzPCFJx19QJ15iWiKnsFK/UBHS+QRA/pAs6XLuOq10BU4F WDewAhoo2sjKiu20I4KXbwOkUVmYPNSwCjTZFClDGvrCPbw9lVlIuliQXwC6r+v6ADJ3RDACC PDDmbKbWI6/CrHNTfZVsTZlThFQj/ym8K8Qsh2CMeOxVsDI93yqdDSyl5O2myJQh96RJfyqQP YDIbeQhqYwZUnoIOEZdEOXRSOoZqGL+vjM3Bp7aR69oR81GApis8PCOlUFi8xahss6OJztGd Received-SPF: pass client-ip=212.227.15.14; envelope-from=arne_bab@web.de; helo=mout.web.de X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17391 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi, In dryads-wake I need selection of the element in a list in a macro from user-input. Currently I have multiple macros, and the correct one (which strips the non-selected choices) is selected in a simple cond: (define-syntax-rule (Choose resp . choices) "Ask questions, apply consequences" (cond ((equal? resp 1) ;; resp is user-input. It is a natural number. (Respond1 choices)) ((equal? resp 2) (Respond2 choices)) ((equal? resp 3) (Respond3 choices)) (else #f))) For this however I have three syntax-case macros: (define-syntax Respond1 (lambda (x) (syntax-case x () ((_ ((question consequences ...) choices ...)) #`(begin (respond consequences ...))) ((_ (choices ...)) #`(begin #f))))) (define-syntax Respond2 (lambda (x) (syntax-case x () ((_ (choice choices ...)) #`(begin (Respond1 (choices ...)))) ((_ (choices ...)) #`(begin #f))))) (define-syntax Respond3 (lambda (x) (syntax-case x () ((_ (a b choices ...)) #`(Respond1 (choices ...))) ((_ (choices ...)) #`(begin #f))))) I would like to get rid of those three definitions and replace them by at most two (one that strips N initial list entries, and Respond1). I cannot move to procedures, because I have code that must be executed only during final processing, and when I evaluate any of the consequences (as it happens with procedure-arguments), then the timing of the code execution does not match anymore. So I must absolutely do this in macros. I=E2=80=99ve tried to get that working, but all my tries failed. Is there a= way and can you show it to me? This is a minimal working example. The output should stay the same, except for part 4, which needs this change to work (see at the bottom), but I would like to: =2D replace Respond2 and Respond3 by something recursive, so resp can have arbitrary high values (not infinite: max the length of the options) and =2D replace the cond-clause by a call to the recursive macro. (define-syntax-rule (respond consequence consequence2 ...) (begin (write consequence) (when (not (null? '(consequence2 ...))) (write (car (cdr (car `(consequence2 ...)))))))) (define-syntax Respond1 (lambda (x) (syntax-case x () ((_ ((question consequences ...) choices ...)) #`(begin (respond consequences ...))) ((_ (choices ...)) #`(begin #f))))) (define-syntax Respond2 (lambda (x) (syntax-case x () ((_ (choice choices ...)) #`(begin (Respond1 (choices ...)))) ((_ (choices ...)) #`(begin #f))))) (define-syntax Respond3 (lambda (x) (syntax-case x () ((_ (a b choices ...)) #`(Respond1 (choices ...))) ((_ (choices ...)) #`(begin #f))))) (define-syntax-rule (Choose resp . choices) "Ask questions, apply consequences" (cond ((equal? resp 1) (Respond1 choices)) ((equal? resp 2) (Respond2 choices)) ((equal? resp 3) (Respond3 choices)) (else #f))) (display "Choose 1: should be bar:") (Choose 1 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) (newline) (display "Choose 2: should be warhar:") (Choose 2 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) (newline) (display "Choose 3: should be mar:") (Choose 3 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) (newline) (display "Choose 4: should be tar:") (Choose 4 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) (newline) (display "Choose 5: should be #f:") (Choose 5 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) (newline) Best wishes, Arne =2D-=20 Unpolitisch sein hei=C3=9Ft politisch sein ohne es zu merken --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQJEBAEBCAAuFiEE801qEjXQSQPNItXAE++NRSQDw+sFAmBq9M8QHGFybmVfYmFi QHdlYi5kZQAKCRAT741FJAPD683XD/4nmdWwKyb2ZEGW9MUc/xDDB1jQSzFhvdzC tYvkp2gZUjmWIWeKERFOnO0usvn0uOOB02a2DNIC/p82MJ4h8+ASDuEy48MDp2qT ZqzSdh3aBdvWDeukhuaoLZgNPSlF6hU2oy3O20HFIadj9ybyfpfUbBR8he4+qRyj aIYVMbT5s/9LNt4b17psn8DwC/ygk4eM3W0YRdArKz+IGnKp4teTK0mx9c7JT9EQ PT/4DqRLt+bW+pynsarheJuv+2762KcaTx9gGf5zDsRBKjjEWy71cFAzZ9pTkmiU fAjWUL8FmQ8F5cjk48QHgk4oODRdPHJmEdDthh7t8wqmcJKdebdabeofxcE/5xxT QdHRCTYVYMGym+DphZCF7WHv0C1jEErmp3YL5d//l6mE5zDwXBkHiKHUQ0HQtAXN zaT6CD4WCKhO/HRJVIPmxkVacJnZrSyEZ99SP0XB/2DalG30yAoVnPITA5v/cWFU qz+gKgk09ZDuOq0bCksbfVP1Pf0PaNoW+JSymT0WFyZ5x9SOHi4uAT2XqAMUF6KW yf/RDTDi4sdzlJPVb56jLR1BFlvpYuxq1FbvVn6R+gyFsV2q0XmJTsq3/z862aUJ Uo+FuOiPbB08ff7GVU2f8i6hai0NkILFnMy8Zlm7T9QdxL7Y8PxkxQPkhu8p3pVa JG7hPnbJ+ojEBAEBCAAuFiEE3Si95tmHXKvOSosd3M8NswvBBUgFAmBq9NMQHGFy bmVfYmFiQHdlYi5kZQAKCRDczw2zC8EFSNUzA/9o7gMUie/4CPwfS9bI+mWyA/jz rDZVK9kVV2SSoznCihGKsL1pfNR6rjoDy+E75J28bW/+9bM7m0swA4QoI1D2+LnP blzEOXRRkmKHe9rBoCJCKGvqNk0Vm4aiReaLuNZJMUQQ8kKkLh1kVwgozON4v4Hk 9o9Xx+vaRLDCM/zLEQ== =l1gK -----END PGP SIGNATURE----- --=-=-=--