From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Maciek Godek" Newsgroups: gmane.lisp.guile.user Subject: Re: Closure? Date: Mon, 14 Jul 2008 03:15:32 +0200 Message-ID: References: <49dd78620807121557n5c7a1f3bs5a2c2788faca21a7@mail.gmail.com> <49dd78620807131556u78b6a3bah9f34e12a5c60c41f@mail.gmail.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1215998148 19432 80.91.229.12 (14 Jul 2008 01:15:48 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 14 Jul 2008 01:15:48 +0000 (UTC) Cc: guile-user@gnu.org To: "Neil Jerram" Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Jul 14 03:16:35 2008 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.50) id 1KICg5-0001bH-Bd for guile-user@m.gmane.org; Mon, 14 Jul 2008 03:16:33 +0200 Original-Received: from localhost ([127.0.0.1]:46717 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KICfD-0000YT-1y for guile-user@m.gmane.org; Sun, 13 Jul 2008 21:15:39 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KICf9-0000Wj-EG for guile-user@gnu.org; Sun, 13 Jul 2008 21:15:35 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KICf8-0000W8-RR for guile-user@gnu.org; Sun, 13 Jul 2008 21:15:35 -0400 Original-Received: from [199.232.76.173] (port=52452 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KICf8-0000W2-Ow for guile-user@gnu.org; Sun, 13 Jul 2008 21:15:34 -0400 Original-Received: from wf-out-1314.google.com ([209.85.200.173]:48064) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KICf8-0007ev-Fd for guile-user@gnu.org; Sun, 13 Jul 2008 21:15:34 -0400 Original-Received: by wf-out-1314.google.com with SMTP id 28so4171567wfc.24 for ; Sun, 13 Jul 2008 18:15:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=pdPlolZvdrXc+vbz/mkkW6U8ML+qy4seu8TLYsncSUc=; b=nniHRgp8ci5mNdy2MXmTrWlZ3szrRUn+AN+YsCdvcOJezDULi0D7G0cJJI8GHpI/Zc u+zcTFMRN4s/rUz1nAMGUGOGpB/FCNcw4EWE0bWPiLEZjlXamLd3ft6SGouAB1XQFo58 2xkR7JKYLMjW0VaGV3NT0EgOj6JUhkCua7xaA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=HIXisSgh86ribKEvF5ojb7Ap/IQFYEyevUEnnLrxkChiSlaJ2a6oeLs4hKI7OIKPiX au1RNZmcNudSQVcqTYawrflgFxAXQxpCNi97DQ3UULgjoUteX2j8CxT9A/vbLr3v6aLO cQ93ndOMwkshTqL+Gc/+i8t8Crvsh51BR+Tm8= Original-Received: by 10.142.52.9 with SMTP id z9mr4001400wfz.70.1215998132633; Sun, 13 Jul 2008 18:15:32 -0700 (PDT) Original-Received: by 10.142.133.13 with HTTP; Sun, 13 Jul 2008 18:15:32 -0700 (PDT) In-Reply-To: <49dd78620807131556u78b6a3bah9f34e12a5c60c41f@mail.gmail.com> Content-Disposition: inline X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 2) 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:6655 Archived-At: >> As the practise shows, although guile documentation says something >> different. In section 3.1.4.7 (A Shared Persistent Variable) >> >> "An important detail here is that the `get-balance' and `deposit' >> variables must be set up by `define'ing them at top level and then >> `set!'ing their values inside the `let' body. Using `define' within >> the `let' body would not work: this would create variable bindings >> within the local `let' environment that would not be accessible at top >> level." >> >> So one might conclude that it _is_ possible to use define inside >> a 'let' form. > > Which would be correct! For example: > > (let ((a 1)) > (define b 2) > (+ a b)) > => > 3 > > Whereas: > > (let ((a 1)) > (display a) > (newline) > (define b 2) > (+ a b)) > => > ERROR: Bad define placement > > The "special rules" are just that any defines have to come before > anything else in the body of the let. Yeah, guess you're right (under certain circumstances :P) > I don't know exactly how it works out that using a define in > local-eval falls foul of the define placement rule, but it is not hard > to imagine that it could do. The other question is: is it really necessary to impose such limitations on "define". Why is it required to make its position inside let privileged? >> Yes, since there's local-eval and the-environment, everything I've >> ever dreamed of is possible :) >> But as I've concluded from the discourse, neither of these is >> defined in R5RS (and it makes me wonder) > > Well I've never thought this through before, but perhaps that is > because in many cases it is equivalent to create a lambda at the point > where you would call the-environment, containing the code that you > would later pass to local-eval. > > For example, the ++ example then becomes: > > (define ++ (let ((c 0)) (lambda () (begin (set! c (+ c 1)) c)))) > > - which is the traditional way of writing this example. You didn't focus :> The whole idea of accessing a closure environment was in fact to make scheme object oriented programming more intuitive. In guile info pages there's an oo closure example: (section 3.1.4.9 "Example 4: Object Orientation") " (define (make-account) (let ((balance 0)) (define (get-balance) balance) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (withdraw amount) (deposit (- amount))) (lambda args (apply (case (car args) ((get-balance) get-balance) ((deposit) deposit) ((withdraw) withdraw) (else (error "Invalid method!"))) (cdr args))))) (define my-account (make-account)) " Notice the ugly "case" statement that requires the variables to be accessed in the following manner (the same example, a few lines later): " (my-account 'get-balance) => 0 (my-account 'withdraw 5) => -5 (my-account 'deposit 396) => 391 (my-account 'get-balance) => 391 " This is ugly as it requires doubling the names of functions. Perhaps it could be overcome with some sort of macro, but the "with" I proposed allows to avoid the whole "case" and to write (after slight modifications in the "let" form): (with my-account (get-balance)) Or maybe I think wrong; I'm new in the world of lisp, so please forgive me my mistakes :) Best regards, M