From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ian Price Newsgroups: gmane.lisp.guile.user Subject: Re: progv in scheme Date: Tue, 13 Sep 2011 18:57:48 +0100 Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1315936776 27306 80.91.229.12 (13 Sep 2011 17:59:36 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 13 Sep 2011 17:59:36 +0000 (UTC) Cc: guile-user@gnu.org To: Panicz Maciej Godek Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Tue Sep 13 19:59:32 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 1R3XGd-0000yi-O5 for guile-user@m.gmane.org; Tue, 13 Sep 2011 19:59:31 +0200 Original-Received: from localhost ([::1]:44553 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R3XGd-0006Dq-Cs for guile-user@m.gmane.org; Tue, 13 Sep 2011 13:59:31 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:52450) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R3XGa-0006Dk-N9 for guile-user@gnu.org; Tue, 13 Sep 2011 13:59:29 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1R3XGZ-0000wP-M6 for guile-user@gnu.org; Tue, 13 Sep 2011 13:59:28 -0400 Original-Received: from mail-ww0-f49.google.com ([74.125.82.49]:55796) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R3XGZ-0000w8-EW for guile-user@gnu.org; Tue, 13 Sep 2011 13:59:27 -0400 Original-Received: by wwp14 with SMTP id 14so860086wwp.30 for ; Tue, 13 Sep 2011 10:59:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=r9aiO4S+1dVOkVKH6E6yRZXQSWz4dzKDDE9COIFAPdI=; b=uERIptnEaSWDTW/vfJYzV70Yf67LcEmnA0yXD4H1zDY4c5gm0C7HbKwr9H12L82Q4Q rskLJAv9DAOrMMLaxkXMYrmTZ8JotPWAlRCVNlgaWRyA57/wboYHR+hND0WrG+Qderpo PEIXw1SKs8wXASB93Nss4EH5PX+NpREYlw4w8= Original-Received: by 10.216.168.70 with SMTP id j48mr1186274wel.86.1315936765287; Tue, 13 Sep 2011 10:59:25 -0700 (PDT) Original-Received: from Kagami.home (host86-182-157-221.range86-182.btcentralplus.com. [86.182.157.221]) by mx.google.com with ESMTPS id fg18sm1221300wbb.24.2011.09.13.10.59.23 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 13 Sep 2011 10:59:24 -0700 (PDT) In-Reply-To: (Panicz Maciej Godek's message of "Tue, 13 Sep 2011 15:54:03 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 74.125.82.49 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:8785 Archived-At: Panicz Maciej Godek writes: > 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. :-) > 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"