From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Panicz Maciej Godek Newsgroups: gmane.lisp.guile.user Subject: Re: progv in scheme Date: Tue, 13 Sep 2011 20:48:59 +0200 Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: dough.gmane.org 1315939750 23932 80.91.229.12 (13 Sep 2011 18:49:10 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 13 Sep 2011 18:49:10 +0000 (UTC) Cc: guile-user@gnu.org To: Ian Price Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Tue Sep 13 20:49:05 2011 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1R3Y2b-0007VQ-9W for guile-user@m.gmane.org; Tue, 13 Sep 2011 20:49:05 +0200 Original-Received: from localhost ([::1]:44986 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R3Y2a-0000kB-OQ for guile-user@m.gmane.org; Tue, 13 Sep 2011 14:49:04 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:47574) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R3Y2X-0000ju-5D for guile-user@gnu.org; Tue, 13 Sep 2011 14:49:02 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1R3Y2V-0005M5-SB for guile-user@gnu.org; Tue, 13 Sep 2011 14:49:01 -0400 Original-Received: from mail-qy0-f169.google.com ([209.85.216.169]:33751) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R3Y2V-0005Lz-Pt for guile-user@gnu.org; Tue, 13 Sep 2011 14:48:59 -0400 Original-Received: by qyl38 with SMTP id 38so2154727qyl.0 for ; Tue, 13 Sep 2011 11:48:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=SciCU1OJIvWUem9cVwErqnrZ0A8P9+ZfTHCxcGy23Xc=; b=BmdSQmNjEMJpcxw52ywYTPD+NDvnbjYhuTzv+S98koaIR6Tj2Z+rnI1yGs6HGQ7g6h 7UWK1vefOPj46ilwxAUkw/sgecyMv0JsiLeRB2mHkJm4SBuw34GLahD/cybLN5wmT5Fu txNJ27EfRdPhRDRIOZsq/xOv55Th7RNEOXH/Y= Original-Received: by 10.220.115.17 with SMTP id g17mr883689vcq.233.1315939739188; Tue, 13 Sep 2011 11:48:59 -0700 (PDT) Original-Received: by 10.220.188.3 with HTTP; Tue, 13 Sep 2011 11:48:59 -0700 (PDT) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 209.85.216.169 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 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.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:8786 Archived-At: 2011/9/13, Ian Price : >> Hello, >> Is there any clever way of binding values to the list of unknown >> symbols in scheme? > I have to admit, I don't understand why you would want to bind values > to a list of _unknown_ symbols; changing random bindings is just asking > for trouble. :-) The existence is asking for trouble :) Actually I've been porting a pattern matcher described by Peter Norvig (in his great "Paradigms of AI Programming") from common lisp to scheme. The pattern matcher differs in many ways from the one provided with guile -- it is not implemented with reader macros and therefore it does not syntactically bind the variables that appear in the pattern forms; instead, it returns an assoc list containing symbols and values. Norvig allows to place arbitrary code within the pattern forms, so a call to eval is inevitable in the long run; I thought, however, that maybe there could be some other way to introduce a variable to a scope. I do like the pattern matcher designed by Andrew K. Wright and written by Alex Shinn, but it lacks some capabilities, and the pattern language is still too difficult for me to apprehend, and so it is hard for me to modify it. (The author didn't allow to use multiple ellipses at one level, because he "didn't want to make it easy to construct very expensive operations", and I don't know how to bypass this limitation) Thanks, M. >> In common lisp there is a form "progv" that takes the list of symbols >> and their corresponding values and binds them within the body of >> progv. > Strictly, this isn't what 'progv' does. 'progv' creates new _dynamic_ > bindings, not lexical ones. e.g. > > (setq *x* 1) > (defun foo () (write *x*)) > (foo) > prints 1 > (progv '(*x*) '(4) (foo)) > prints 4 > >> It is possible to do it using eval, like this: >> (define (bind-and-eval symbols values body) >> (eval `((lambda ,symbols ,body) . ,values) >> (interaction-environment))) >> (define-syntax let-symbols >> (syntax-rules () >> ((_ symbols values (body ...)) >> (bind-and-eval symbols values (quote (body ...)))))) >> >> but using eval for this just seems too heavy. Is there any way of >> doing it that would be more legal? > Not really, doing so would break lexical scope. In a lexically > scoped language, the symbol isn't what matters; the "location" is. An > implementation is free to rename your identifiers, and once you reach > run-time, all the names have been forgotten anyway. Of course, there are > 'unnatural' ways of expressing this, as you did with 'eval'. > > If you know the list before hand, you can use match. > > (use-modules (ice-9 match) > > (match (list 1 2 3) > ((a b c) (list 'success b c a)) > (else 'fail)) > ; => (success 2 3 1) > > If you want 'dynamic variables', then Scheme does not provide them, but > there are 'fluids' or 'parameters' which behave similarly. > > (define x (make-fluid)) > > (define (print) > (write (fluid-ref x))) > > (cons (fluid-ref x) > (with-fluids* (list x) '(rebound) > (lambda () (print) (fluid-ref x)))) > ;; => (#f . rebound) > ;; prints 'rebound > > Hope that helps > > -- > Ian Price > > "Programming is like pinball. The reward for doing it well is > the opportunity to do it again" - from "The Wizardy Compiled" >