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: Re: Best way to call a user defined hook (written in guile) from C when the hook need plenty parameters Date: Mon, 05 Jul 2010 11:57:51 +0200 Message-ID: <87wrtantcw.fsf@ambire.localdomain> References: <20100705085635.GB9492@apc> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1278324079 28533 80.91.229.12 (5 Jul 2010 10:01:19 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 5 Jul 2010 10:01:19 +0000 (UTC) Cc: guile-user@gnu.org To: rixed@happyleptic.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Jul 05 12:01:15 2010 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.69) (envelope-from ) id 1OViUE-0002T6-MR for guile-user@m.gmane.org; Mon, 05 Jul 2010 12:01:14 +0200 Original-Received: from localhost ([127.0.0.1]:54396 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OViUE-0007ht-8m for guile-user@m.gmane.org; Mon, 05 Jul 2010 06:01:14 -0400 Original-Received: from [140.186.70.92] (port=40342 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OViTu-0007ho-VC for guile-user@gnu.org; Mon, 05 Jul 2010 06:00:56 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OViTq-0000rw-Jy for guile-user@gnu.org; Mon, 05 Jul 2010 06:00:54 -0400 Original-Received: from smtp206.alice.it ([82.57.200.102]:48368) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OViTq-0000rZ-AO for guile-user@gnu.org; Mon, 05 Jul 2010 06:00:50 -0400 Original-Received: from ambire.localdomain (95.236.70.235) by smtp206.alice.it (8.5.124.08) id 4C1A268C012950CB; Mon, 5 Jul 2010 12:00:25 +0200 Original-Received: from ttn by ambire.localdomain with local (Exim 4.69) (envelope-from ) id 1OViQx-0001oZ-Pf; Mon, 05 Jul 2010 11:57:51 +0200 In-Reply-To: <20100705085635.GB9492@apc> (rixed@happyleptic.org's message of "Mon, 5 Jul 2010 10:56:36 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. 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:7958 Archived-At: () rixed@happyleptic.org () Mon, 5 Jul 2010 10:56:36 +0200 Can anyone suggest a better way to do this ? For "a set of named values", you can use an association list. If the paren verbosity is off-putting, a common way is to take/show plists externally (minimal (one) set of parens). So, user sees: (k1 v1 k2 v2 ...) which is not so threatening, but you manipulate internally: =20 ((k1 . v1) (k2 . v2) ...) This requires a transform, but you do that anyway (as part of validating the user input), right? All this presumes that there is no need to specify all keys all the time. (Re-reading your post, perhaps i am misunderstanding the problem question.) If the problem really is: how to avoid unwieldy argument lists, the answer is still "use an association list", but pass to the user a procedure that encapsulates it. For example: (define (query-proc alist) "Encapsulate ALIST; return a procedure to query it." (lambda (key) (assoc-ref alist key))) (define ALIST '((k1 . v1) (k2 . v2))) (define QUERY (query-proc ALIST)) ;; On the user side: (define (monitor query) (for-each (lambda (key) (simple-format #t "k: ~S~%v: ~S~%" key (query key))) '(k1 k2))) This example is read-only; if you want =E2=80=98monitor=E2=80=99 to be able= to munge you can change =E2=80=98query-proc=E2=80=99 to perhaps =E2=80=98query/munge= -proc=E2=80=99: (define (query/munge-proc alist) "Encapsulate ALIST; return a procedure query/munge it." (lambda (key . newval) (if (null? newval) (assoc-ref alist key) (set! alist (assoc-set! alist key (car newval)))))) =20=20=20=20=20=20=20=20 (It all depends on how much you trust the users. ;-) thi